|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#[cfg(test)] |
| 5 | +mod tests; |
| 6 | + |
| 7 | +use crate::qsc_utils::{map_offset, span_contains, Compilation}; |
| 8 | +use qsc::hir::{ |
| 9 | + visit::{walk_item, Visitor}, |
| 10 | + ItemKind, {Block, Item, Package}, |
| 11 | +}; |
| 12 | +use std::collections::HashSet; |
| 13 | + |
| 14 | +// It would have been nice to match these enum values to the ones used by |
| 15 | +// VS Code and Monaco, but unfortunately those two disagree on the values. |
| 16 | +// So we define our own unique enum here to reduce confusion. |
| 17 | +#[derive(Clone, Debug, PartialEq)] |
| 18 | +#[allow(clippy::module_name_repetitions)] |
| 19 | +pub enum CompletionItemKind { |
| 20 | + Function, |
| 21 | + Module, |
| 22 | + Keyword, |
| 23 | + Issue, |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Debug)] |
| 27 | +#[allow(clippy::module_name_repetitions)] |
| 28 | +pub struct CompletionList { |
| 29 | + pub items: Vec<CompletionItem>, |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Clone, Debug, PartialEq)] |
| 33 | +#[allow(clippy::module_name_repetitions)] |
| 34 | +pub struct CompletionItem { |
| 35 | + pub label: String, |
| 36 | + pub kind: CompletionItemKind, |
| 37 | +} |
| 38 | + |
| 39 | +pub(crate) fn get_completions( |
| 40 | + compilation: &Compilation, |
| 41 | + source_name: &str, |
| 42 | + offset: u32, |
| 43 | +) -> CompletionList { |
| 44 | + // Map the file offset into a SourceMap offset |
| 45 | + let offset = map_offset(&compilation.source_map, source_name, offset); |
| 46 | + let package = &compilation.package; |
| 47 | + let std_package = &compilation |
| 48 | + .package_store |
| 49 | + .get(compilation.std_package_id) |
| 50 | + .expect("expected to find std package") |
| 51 | + .package; |
| 52 | + |
| 53 | + // Collect namespaces |
| 54 | + let mut namespace_collector = NamespaceCollector { |
| 55 | + namespaces: HashSet::new(), |
| 56 | + }; |
| 57 | + namespace_collector.visit_package(package); |
| 58 | + namespace_collector.visit_package(std_package); |
| 59 | + |
| 60 | + // All namespaces |
| 61 | + let mut namespaces = namespace_collector |
| 62 | + .namespaces |
| 63 | + .drain() |
| 64 | + .map(|ns| CompletionItem { |
| 65 | + label: ns, |
| 66 | + kind: CompletionItemKind::Module, |
| 67 | + }) |
| 68 | + .collect::<Vec<_>>(); |
| 69 | + |
| 70 | + // Determine context for the offset |
| 71 | + let mut context_builder = ContextFinder { |
| 72 | + offset, |
| 73 | + context: if compilation.package.items.values().next().is_none() { |
| 74 | + Context::NoCompilation |
| 75 | + } else { |
| 76 | + Context::TopLevel |
| 77 | + }, |
| 78 | + }; |
| 79 | + context_builder.visit_package(package); |
| 80 | + let context = context_builder.context; |
| 81 | + |
| 82 | + let mut items = Vec::new(); |
| 83 | + match context { |
| 84 | + Context::Namespace => { |
| 85 | + items.push(CompletionItem { |
| 86 | + label: "open".to_string(), |
| 87 | + kind: CompletionItemKind::Keyword, |
| 88 | + }); |
| 89 | + items.append(&mut namespaces); |
| 90 | + } |
| 91 | + Context::Block | Context::NoCompilation => { |
| 92 | + // Add everything we know of. |
| 93 | + // All callables from std package |
| 94 | + items.append(&mut callable_names_from_package(std_package)); |
| 95 | + // Callables from the current document |
| 96 | + items.append(&mut callable_names_from_package(package)); |
| 97 | + items.append(&mut namespaces); |
| 98 | + } |
| 99 | + Context::TopLevel | Context::NotSignificant => items.push(CompletionItem { |
| 100 | + label: "namespace".to_string(), |
| 101 | + kind: CompletionItemKind::Keyword, |
| 102 | + }), |
| 103 | + } |
| 104 | + CompletionList { items } |
| 105 | +} |
| 106 | + |
| 107 | +struct NamespaceCollector { |
| 108 | + namespaces: HashSet<String>, |
| 109 | +} |
| 110 | + |
| 111 | +impl Visitor<'_> for NamespaceCollector { |
| 112 | + fn visit_item(&mut self, item: &Item) { |
| 113 | + if let ItemKind::Namespace(ident, _) = &item.kind { |
| 114 | + // Collect namespaces |
| 115 | + self.namespaces.insert(ident.name.to_string()); |
| 116 | + } |
| 117 | + walk_item(self, item); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +struct ContextFinder { |
| 122 | + offset: u32, |
| 123 | + context: Context, |
| 124 | +} |
| 125 | + |
| 126 | +#[derive(Debug, PartialEq)] |
| 127 | +enum Context { |
| 128 | + NoCompilation, |
| 129 | + TopLevel, |
| 130 | + Namespace, |
| 131 | + Block, |
| 132 | + NotSignificant, |
| 133 | +} |
| 134 | + |
| 135 | +impl Visitor<'_> for ContextFinder { |
| 136 | + fn visit_item(&mut self, item: &Item) { |
| 137 | + if span_contains(item.span, self.offset) { |
| 138 | + self.context = match &item.kind { |
| 139 | + ItemKind::Namespace(..) => Context::Namespace, |
| 140 | + _ => Context::NotSignificant, |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + walk_item(self, item); |
| 145 | + } |
| 146 | + |
| 147 | + fn visit_block(&mut self, block: &Block) { |
| 148 | + if span_contains(block.span, self.offset) { |
| 149 | + self.context = Context::Block; |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +fn callable_names_from_package(package: &Package) -> Vec<CompletionItem> { |
| 155 | + package |
| 156 | + .items |
| 157 | + .values() |
| 158 | + .filter_map(|i| match &i.kind { |
| 159 | + ItemKind::Callable(callable_decl) => Some(CompletionItem { |
| 160 | + label: callable_decl.name.name.to_string(), |
| 161 | + kind: CompletionItemKind::Function, |
| 162 | + }), |
| 163 | + _ => None, |
| 164 | + }) |
| 165 | + .collect::<Vec<_>>() |
| 166 | +} |
0 commit comments