|
1 |
| -// The module 'vscode' contains the VS Code extensibility API |
2 |
| -// Import the module and reference it with the alias vscode in your code below |
| 1 | +import * as ts from "typescript"; |
3 | 2 | import * as vscode from "vscode";
|
| 3 | +import { |
| 4 | + postInsertedScript, |
| 5 | + preInsertedScript, |
| 6 | + triggerTextTsToYaml, |
| 7 | + triggerTextYamlToTs, |
| 8 | +} from "./Contract"; |
| 9 | +import { logger } from "./Logger"; |
| 10 | +import { createLanguageService, updateScript } from "./LanguageService"; |
| 11 | +import { asCompletionItemKind } from "./Platform"; |
4 | 12 |
|
5 |
| -// This method is called when your extension is activated |
6 |
| -// Your extension is activated the very first time the command is executed |
7 |
| -export function activate(context: vscode.ExtensionContext) { |
8 |
| - // Use the console to output diagnostic information (console.log) and errors (console.error) |
9 |
| - // This line of code will only be executed once when your extension is activated |
10 |
| - console.log( |
11 |
| - 'Congratulations, your extension "workflow-script-highlighter" is now active!' |
| 13 | +// For debugging |
| 14 | +logger.hide(); |
| 15 | + |
| 16 | +let languageService: ts.LanguageService; |
| 17 | + |
| 18 | +function extractTsCodeBlock( |
| 19 | + text: string, |
| 20 | + offset: number |
| 21 | +): { content: string; offset: number } | null { |
| 22 | + const tsBlockStart = text.lastIndexOf(triggerTextYamlToTs, offset); |
| 23 | + const tsBlockEnd = text.indexOf( |
| 24 | + triggerTextTsToYaml, |
| 25 | + tsBlockStart + triggerTextYamlToTs.length |
12 | 26 | );
|
13 | 27 |
|
14 |
| - // The command has been defined in the package.json file |
15 |
| - // Now provide the implementation of the command with registerCommand |
16 |
| - // The commandId parameter must match the command field in package.json |
17 |
| - const disposable = vscode.commands.registerCommand( |
18 |
| - "workflow-script-highlighter.helloWorld", |
19 |
| - () => { |
20 |
| - // The code you place here will be executed every time your command is executed |
21 |
| - // Display a message box to the user |
22 |
| - vscode.window.showInformationMessage("Hello World from !"); |
23 |
| - } |
| 28 | + if (tsBlockStart === -1 || tsBlockEnd === -1 || tsBlockEnd < offset) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + |
| 32 | + const content = text.substring( |
| 33 | + tsBlockStart + triggerTextYamlToTs.length, |
| 34 | + tsBlockEnd - postInsertedScript.length |
24 | 35 | );
|
| 36 | + const tsOffset = offset - (tsBlockStart + triggerTextYamlToTs.length); |
| 37 | + |
| 38 | + return { content, offset: tsOffset }; |
| 39 | +} |
| 40 | + |
| 41 | +export function activate(context: vscode.ExtensionContext) { |
| 42 | + languageService = createLanguageService(); |
| 43 | + |
| 44 | + const completionItemProvider: vscode.CompletionItemProvider = { |
| 45 | + provideCompletionItems( |
| 46 | + document: vscode.TextDocument, |
| 47 | + position: vscode.Position |
| 48 | + ) { |
| 49 | + const text = document.getText(); |
| 50 | + const offset = document.offsetAt(position); |
25 | 51 |
|
26 |
| - context.subscriptions.push(disposable); |
| 52 | + const tsCodeBlock = extractTsCodeBlock(text, offset); |
| 53 | + if (!tsCodeBlock) { |
| 54 | + return []; |
| 55 | + } |
| 56 | + |
| 57 | + const scriptFileName = document.uri.fsPath + ".ts"; |
| 58 | + const augmentedContent = `${preInsertedScript}${tsCodeBlock.content}${postInsertedScript}`; |
| 59 | + updateScript(scriptFileName, augmentedContent); |
| 60 | + |
| 61 | + const updatedPosition = tsCodeBlock.offset + preInsertedScript.length; |
| 62 | + |
| 63 | + const completions = languageService.getCompletionsAtPosition( |
| 64 | + scriptFileName, |
| 65 | + updatedPosition, |
| 66 | + {} |
| 67 | + ); |
| 68 | + if (!completions) { |
| 69 | + return []; |
| 70 | + } |
| 71 | + |
| 72 | + // TODO: More powerful completion |
| 73 | + const mapped = completions.entries.map((entry) => { |
| 74 | + const item = new vscode.CompletionItem( |
| 75 | + entry.name, |
| 76 | + asCompletionItemKind(entry.kind) |
| 77 | + ); |
| 78 | + item.sortText = entry.sortText; |
| 79 | + |
| 80 | + return item; |
| 81 | + }); |
| 82 | + |
| 83 | + return mapped; |
| 84 | + }, |
| 85 | + }; |
| 86 | + |
| 87 | + const githubActionsWorkflowProvider = |
| 88 | + vscode.languages.registerCompletionItemProvider( |
| 89 | + { language: "github-actions-workflow", scheme: "file" }, |
| 90 | + completionItemProvider, |
| 91 | + "." |
| 92 | + ); |
| 93 | + |
| 94 | + context.subscriptions.push(githubActionsWorkflowProvider); |
27 | 95 | }
|
28 | 96 |
|
29 |
| -// This method is called when your extension is deactivated |
30 |
| -export function deactivate() {} |
| 97 | +export function deactivate() { |
| 98 | + if (languageService) { |
| 99 | + languageService.dispose(); |
| 100 | + } |
| 101 | +} |
0 commit comments