microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/language_service/src/completion/locals.rs
112lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use super::Completion; |
| 5 | use crate::{compilation::Compilation, protocol::CompletionItemKind}; |
| 6 | use qsc::{ |
| 7 | display::{CodeDisplay, Lookup}, |
| 8 | hir::ItemKind, |
| 9 | resolve::Local, |
| 10 | }; |
| 11 | |
| 12 | /// Provides the locals that are visible at the cursor offset |
| 13 | pub(super) struct Locals<'a> { |
| 14 | compilation: &'a Compilation, |
| 15 | offset: u32, |
| 16 | } |
| 17 | |
| 18 | impl Locals<'_> { |
| 19 | pub fn new(offset: u32, compilation: &Compilation) -> Locals { |
| 20 | Locals { |
| 21 | compilation, |
| 22 | offset, |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | pub fn expr_names(&self) -> Vec<Completion> { |
| 27 | Self::local_completions(self.compilation, self.offset, true, false) |
| 28 | } |
| 29 | |
| 30 | pub fn type_names(&self) -> Vec<Completion> { |
| 31 | Self::local_completions(self.compilation, self.offset, false, true) |
| 32 | } |
| 33 | |
| 34 | fn local_completions( |
| 35 | compilation: &Compilation, |
| 36 | offset: u32, |
| 37 | include_terms: bool, |
| 38 | include_tys: bool, |
| 39 | ) -> Vec<Completion> { |
| 40 | compilation |
| 41 | .user_unit() |
| 42 | .ast |
| 43 | .locals |
| 44 | .get_all_at_offset(offset) |
| 45 | .iter() |
| 46 | .filter_map(|candidate| { |
| 47 | Self::local_completion(candidate, compilation, include_terms, include_tys) |
| 48 | }) |
| 49 | .collect::<Vec<_>>() |
| 50 | } |
| 51 | |
| 52 | /// Convert a local into a completion item |
| 53 | fn local_completion( |
| 54 | candidate: &Local, |
| 55 | compilation: &Compilation, |
| 56 | include_terms: bool, |
| 57 | include_tys: bool, |
| 58 | ) -> Option<Completion> { |
| 59 | let display = CodeDisplay { compilation }; |
| 60 | let (kind, detail) = match &candidate { |
| 61 | Local::Item(item_id, _) => { |
| 62 | let item = compilation.resolve_item_relative_to_user_package(item_id); |
| 63 | let (detail, kind) = match &item.0.kind { |
| 64 | ItemKind::Callable(decl) => { |
| 65 | if !include_terms { |
| 66 | return None; |
| 67 | } |
| 68 | ( |
| 69 | Some(display.hir_callable_decl(decl).to_string()), |
| 70 | CompletionItemKind::Function, |
| 71 | ) |
| 72 | } |
| 73 | ItemKind::Namespace(_, _) => { |
| 74 | panic!("did not expect local namespace item") |
| 75 | } |
| 76 | ItemKind::Ty(_, udt) => { |
| 77 | if !include_terms && !include_tys { |
| 78 | return None; |
| 79 | } |
| 80 | ( |
| 81 | Some(display.hir_udt(udt).to_string()), |
| 82 | CompletionItemKind::Interface, |
| 83 | ) |
| 84 | } |
| 85 | // We don't want completions for items exported from the local scope |
| 86 | ItemKind::Export(_, _) => return None, |
| 87 | }; |
| 88 | (kind, detail) |
| 89 | } |
| 90 | Local::Var(node_id, name) => { |
| 91 | if !include_terms { |
| 92 | return None; |
| 93 | } |
| 94 | let detail = Some(display.name_ty_id(name, *node_id).to_string()); |
| 95 | (CompletionItemKind::Variable, detail) |
| 96 | } |
| 97 | Local::TyParam(..) => { |
| 98 | if !include_tys { |
| 99 | return None; |
| 100 | } |
| 101 | (CompletionItemKind::TypeParameter, None) |
| 102 | } |
| 103 | Local::NamespaceImport(..) => return None, |
| 104 | }; |
| 105 | |
| 106 | Some(Completion::with_detail( |
| 107 | candidate.name().expect("expected item name").to_string(), |
| 108 | kind, |
| 109 | detail, |
| 110 | )) |
| 111 | } |
| 112 | } |
| 113 | |