Skip to content

Commit b222be9

Browse files
committed
Standardise APIs for token manipulation
Signed-off-by: worksofliam <[email protected]>
1 parent 3387350 commit b222be9

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

extension/server/src/providers/completionItem.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,18 @@ export default async function completionItemProvider(handler: CompletionParams):
4848
// This means we're just looking for subfields in the struct
4949
if (trigger === `.`) {
5050
const cursorIndex = handler.position.character;
51-
let tokens = Parser.lineTokens(isFree ? currentLine : currentLine.length >= 7 ? currentLine.substring(7) : ``, 0, 0, true);
51+
let tokens = Parser.lineTokens(isFree ? currentLine : currentLine.length >= 7 ? ``.padEnd(7) + currentLine.substring(7) : ``, 0, 0, true);
5252

5353
if (tokens.length > 0) {
5454

5555
// We need to find the innermost block we are part of
56-
let insideBlock: Token | undefined;
57-
while (insideBlock = tokens.find(t => t.type === `block` && t.block && t.range.start <= cursorIndex && t.range.end >= cursorIndex)) {
58-
tokens = insideBlock.block || [];
59-
}
56+
tokens = Parser.fromBlocksGetTokens(tokens, cursorIndex);
6057

61-
let tokenIndex = tokens.findIndex(token => cursorIndex > token.range.start && cursorIndex <= token.range.end);
62-
console.log(tokens);
63-
console.log({ cPos: handler.position.character, tokenIndex });
58+
// Remove any tokens after the cursor
59+
tokens = tokens.filter(token => token.range.end <= cursorIndex);
6460

65-
let lastToken: Token|undefined;
66-
while (tokens[tokenIndex] && [`block`, `word`, `dot`].includes(tokens[tokenIndex].type) && lastToken?.type !== tokens[tokenIndex].type && tokenIndex > 0) {
67-
lastToken = tokens[tokenIndex];
68-
tokenIndex--;
69-
}
61+
// Get the possible variable we're referring to
62+
let tokenIndex = Parser.getReference(tokens, cursorIndex);
7063

7164
let currentDef: Declaration | undefined;
7265

language/parser.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,28 @@ export default class Parser {
159159
return tokens;
160160
}
161161

162+
static fromBlocksGetTokens(tokensWithBlocks: Token[], cursorIndex: number) {
163+
let insideBlock: Token | undefined;
164+
165+
while (insideBlock = tokensWithBlocks.find(t => t.type === `block` && t.block && t.range.start <= cursorIndex && t.range.end >= cursorIndex)) {
166+
tokensWithBlocks = insideBlock.block || [];
167+
}
168+
169+
return tokensWithBlocks;
170+
}
171+
172+
static getReference(tokens: Token[], cursorIndex: number): number|-1 {
173+
let tokenIndex = tokens.findIndex(token => cursorIndex > token.range.start && cursorIndex <= token.range.end);
174+
175+
let lastToken: Token|undefined;
176+
while (tokens[tokenIndex] && [`block`, `word`, `dot`].includes(tokens[tokenIndex].type) && lastToken?.type !== tokens[tokenIndex].type && tokenIndex > 0) {
177+
lastToken = tokens[tokenIndex];
178+
tokenIndex--;
179+
}
180+
181+
return tokenIndex;
182+
}
183+
162184
async getDocs(workingUri: string, baseContent?: string, options: ParseOptions = {withIncludes: true, collectReferences: true}): Promise<Cache|undefined> {
163185
const existingCache = this.getParsedCache(workingUri);
164186
if (options.ignoreCache !== true && existingCache) {

language/tokens.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ export function createBlocks(tokens: Token[]) {
472472
block: createBlocks(newTokens),
473473
range: {
474474
line: tokens[start].range.line,
475-
start: tokens[i].range.start,
475+
start: tokens[start].range.start,
476476
end: tokens[i].range.end,
477477
}
478478
});

0 commit comments

Comments
 (0)