microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
logo

Branches

Tags

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

Clone

HTTPS

Download ZIP

source/language_service/src/completion/fields.rs

66lines · modeblame

29145c51Mine Starks1 years ago1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
e938e457orpuente-MS1 years ago4use super::{Completion, ast_context::AstContext};
29145c51Mine Starks1 years ago5use crate::{compilation::Compilation, protocol::CompletionItemKind};
6use qsc::{
7display::Lookup,
8hir::{
9ItemKind, Res,
e938e457orpuente-MS1 years ago10ty::{Ty, UdtDefKind},
29145c51Mine Starks1 years ago11},
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> {
17compilation: &'a Compilation,
d591b52aMine Starks1 years ago18ast_context: &'a AstContext<'a>,
29145c51Mine Starks1 years ago19}
20
21impl<'a> Fields<'a> {
d591b52aMine Starks1 years ago22pub(crate) fn new(compilation: &'a Compilation, ast_context: &'a AstContext) -> Self {
29145c51Mine Starks1 years ago23Self {
24compilation,
d591b52aMine Starks1 years ago25ast_context,
29145c51Mine Starks1 years ago26}
27}
28
29pub(crate) fn fields(&self) -> Vec<Completion> {
d591b52aMine Starks1 years ago30let Some(id) = self.ast_context.field_access_context() else {
29145c51Mine Starks1 years ago31return vec![];
32};
33
34let mut completions = vec![];
35let ty = self.compilation.get_ty(id);
36if let Some(Ty::Udt(_, Res::Item(item_id))) = ty {
37let (item, _, _) = self
38.compilation
39.resolve_item_relative_to_user_package(item_id);
40if let ItemKind::Ty(_, udt) = &item.kind {
41collect_fields(&mut completions, &udt.definition.kind);
42}
43}
44completions
45}
46}
47
48fn collect_fields(completions: &mut Vec<Completion>, field: &UdtDefKind) {
49match field {
50UdtDefKind::Field(field) => {
51if let Some(name) = &field.name {
52let detail = field.ty.display();
53completions.push(Completion::with_detail(
54name.to_string(),
55CompletionItemKind::Field,
56Some(detail),
57));
58}
59}
60UdtDefKind::Tuple(vec) => {
61for f in vec {
62collect_fields(completions, &f.kind);
63}
64}
65}
66}