-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathautoCompleteProvider.ts
More file actions
82 lines (69 loc) · 2.97 KB
/
Copy pathautoCompleteProvider.ts
File metadata and controls
82 lines (69 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import * as vscode from 'vscode';
import { getWorkspaceSettingsForFile, getWorkspaceUriForFile, sepr } from '../common';
import { config } from '../configuration';
import { featureFileStepRe } from "../parsers/featureParser";
import { getStepFileSteps } from '../parsers/stepsParser';
export const autoCompleteProvider = {
provideCompletionItems(document: vscode.TextDocument, position: vscode.Position): vscode.CompletionItem[] | undefined {
try {
const line = document.lineAt(position);
const lcLine = line.text.trimStart().toLowerCase();
const step = featureFileStepRe.exec(lcLine);
if (!step)
return;
const wkspSettings = getWorkspaceSettingsForFile(document.uri);
if (!wkspSettings)
return;
const stepType = step[1].trim();
const textWithoutType = step[2].trim();
let matchText1 = `^${stepType}${sepr}${textWithoutType}`;
const matchText2 = `^step${sepr}${textWithoutType}`;
if (stepType === "and" || stepType === "but") {
for (let i = position.line - 1; i > -1; i--) {
const lcPrevLine = document.lineAt(i).text.trimStart().toLowerCase();
const prevStep = featureFileStepRe.exec(lcPrevLine);
if (prevStep && prevStep[1].trim() !== "and" && prevStep[1].trim() !== "but") {
const prevStepType = prevStep[1].trim();
matchText1 = `^${prevStepType}${sepr}${textWithoutType}`;
break;
}
}
}
const stepFileSteps = getStepFileSteps(wkspSettings.featuresUri);
const items: vscode.CompletionItem[] = [];
// Calculate the range to replace: from after the step keyword to the end of line
const leadingWhitespace = line.text.length - line.text.trimStart().length;
const stepKeywordEnd = leadingWhitespace + stepType.length + 1;
const replaceRange = new vscode.Range(
position.line,
stepKeywordEnd,
position.line,
line.text.length
);
for (const [key, value] of stepFileSteps) {
const lcKey = key.toLowerCase();
if (lcKey.startsWith(matchText1) || lcKey.startsWith(matchText2)) {
let itemText = value.textAsRe.startsWith(".*") ? " " + value.textAsRe.slice(1) : value.textAsRe;
itemText = value.textAsRe.replaceAll(".*", "?");
// deal with e.g \( escapes in textAsRe
itemText = itemText.replaceAll("\\\\", "#@slash@#").replaceAll("\\", "").replaceAll("#@slash@#", "\\");
const item = new vscode.CompletionItem(itemText, vscode.CompletionItemKind.Function);
item.detail = vscode.workspace.asRelativePath(value.uri);
item.range = replaceRange;
items.push(item);
}
}
return items;
}
catch (e: unknown) {
// entry point function (handler) - show error
try {
const wkspUri = getWorkspaceUriForFile(document.uri);
config.logger.showError(e, wkspUri);
}
catch {
config.logger.showError(e);
}
}
}
}