microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.23.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/language_service/src/completion/fields.rs

66lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use super::{Completion, ast_context::AstContext};
5use crate::{compilation::Compilation, protocol::CompletionItemKind};
6use qsc::{
7 display::Lookup,
8 hir::{
9 ItemKind, Res,
10 ty::{Ty, UdtDefKind},
11 },
12};
13
14/// If there is an incomplete field access expression (e.g. `foo.bar.`) at the cursor offset,
15/// provides the possible field names.
16pub(super) struct Fields<'a> {
17 compilation: &'a Compilation,
18 ast_context: &'a AstContext<'a>,
19}
20
21impl<'a> Fields<'a> {
22 pub(crate) fn new(compilation: &'a Compilation, ast_context: &'a AstContext) -> Self {
23 Self {
24 compilation,
25 ast_context,
26 }
27 }
28
29 pub(crate) fn fields(&self) -> Vec<Completion> {
30 let Some(id) = self.ast_context.field_access_context() else {
31 return vec![];
32 };
33
34 let mut completions = vec![];
35 let ty = self.compilation.get_ty(id);
36 if let Some(Ty::Udt(_, Res::Item(item_id))) = ty {
37 let (item, _, _) = self
38 .compilation
39 .resolve_item_relative_to_user_package(item_id);
40 if let ItemKind::Ty(_, udt) = &item.kind {
41 collect_fields(&mut completions, &udt.definition.kind);
42 }
43 }
44 completions
45 }
46}
47
48fn collect_fields(completions: &mut Vec<Completion>, field: &UdtDefKind) {
49 match field {
50 UdtDefKind::Field(field) => {
51 if let Some(name) = &field.name {
52 let detail = field.ty.display();
53 completions.push(Completion::with_detail(
54 name.to_string(),
55 CompletionItemKind::Field,
56 Some(detail),
57 ));
58 }
59 }
60 UdtDefKind::Tuple(vec) => {
61 for f in vec {
62 collect_fields(completions, &f.kind);
63 }
64 }
65 }
66}
67