Skip to main content

runec/ide/
hints.rs

1use super::{InlayHint, SymbolKind, index};
2use crate::project::{Project, SourceId};
3use crate::token::Kind;
4use std::ops::Range;
5
6impl Project {
7    pub fn inlay_hints(&self, source: SourceId, range: Range<usize>) -> Vec<InlayHint> {
8        let Some(document) = self.document(source) else {
9            return Vec::new();
10        };
11        if document.config.is_some() {
12            return Vec::new();
13        }
14        let index = self.symbol_index();
15        let hooks = index.hooks.get(&source).map_or(&[][..], Vec::as_slice);
16        let tokens = index::tokens(document, &index.compounds, hooks);
17        let mut hints = Vec::new();
18        for reference in index
19            .references
20            .iter()
21            .filter(|reference| reference.location.source == source && !reference.declaration)
22        {
23            let span = &reference.location.range;
24            if !range.contains(&span.start)
25                || document
26                    .tokens
27                    .iter()
28                    .zip(&document.spans)
29                    .any(|(token, range)| {
30                        matches!(token.kind, Kind::StringLiteral | Kind::RawStringLiteral)
31                            && range.contains(&span.start)
32                            && !hooks.iter().any(|hook| hook.range == *range)
33                    })
34            {
35                continue;
36            }
37            let symbol = &index.symbols[reference.symbol];
38            if symbol.kind == SymbolKind::Constant {
39                if let Some(ty) = symbol.value_type {
40                    hints.push(InlayHint {
41                        offset: span.end,
42                        label: format!(": {}", ty.name()),
43                        parameter: false,
44                    });
45                }
46                continue;
47            }
48            if !matches!(
49                symbol.kind,
50                SymbolKind::Command
51                    | SymbolKind::Procedure
52                    | SymbolKind::Trigger
53                    | SymbolKind::ClientScript
54            ) {
55                continue;
56            }
57            let next = tokens.partition_point(|(_, range)| range.start < span.end);
58            let mut end = span.end;
59            if tokens
60                .get(next)
61                .is_some_and(|(token, _)| token.kind == Kind::LParen)
62            {
63                let mut depth = 0;
64                let mut argument = 0;
65                let mut argument_start = true;
66                let mut literal_end = 0;
67                for (token, token_range) in &tokens[next..] {
68                    if token_range.start < literal_end {
69                        continue;
70                    }
71                    if matches!(token.kind, Kind::StringLiteral | Kind::RawStringLiteral) {
72                        literal_end = token_range.end;
73                    }
74                    if depth == 1
75                        && argument_start
76                        && !matches!(
77                            token.kind,
78                            Kind::RParen | Kind::SingleLineComment | Kind::MultiLineComment
79                        )
80                    {
81                        if let Some((name, _)) = symbol.parameters.get(argument) {
82                            hints.push(InlayHint {
83                                offset: token_range.start,
84                                label: format!("{}:", name.trim_start_matches('$')),
85                                parameter: true,
86                            });
87                        }
88                        argument_start = false;
89                    }
90                    match token.kind {
91                        Kind::LParen => depth += 1,
92                        Kind::RParen => {
93                            depth -= 1;
94                            if depth == 0 {
95                                end = token_range.end;
96                                break;
97                            }
98                        }
99                        Kind::Comma if depth == 1 => {
100                            argument += 1;
101                            argument_start = true;
102                        }
103                        _ => {}
104                    }
105                }
106            } else if !symbol.parameters.is_empty() {
107                continue;
108            }
109            if !symbol.returns.is_empty() {
110                hints.push(InlayHint {
111                    offset: end,
112                    label: format!(
113                        "-> {}",
114                        symbol
115                            .returns
116                            .iter()
117                            .map(|ty| ty.name())
118                            .collect::<Vec<_>>()
119                            .join(", ")
120                    ),
121                    parameter: false,
122                });
123            }
124        }
125        hints.sort_by_key(|hint| hint.offset);
126        hints
127    }
128}