|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { CompletionItemProvider, TextDocument, Position, CancellationToken, CompletionContext, CompletionItem, CompletionItemKind, SnippetString, MarkdownString } from "vscode"; |
| 4 | +import * as fse from 'fs-extra'; |
| 5 | +import * as path from 'path'; |
| 6 | + |
| 7 | +export class SnippetCompletionProvider implements CompletionItemProvider { |
| 8 | + |
| 9 | + private snippets: {}; |
| 10 | + private activation: boolean; |
| 11 | + |
| 12 | + public constructor() { |
| 13 | + this.activation = true; |
| 14 | + this.snippets = fse.readJSONSync(path.join(__dirname, '..', 'snippets', 'server.json')); |
| 15 | + } |
| 16 | + |
| 17 | + public async provideCompletionItems(_document: TextDocument, _position: Position, _token: CancellationToken, _context: CompletionContext): Promise<CompletionItem[]> { |
| 18 | + if (!this.activation) { |
| 19 | + return []; |
| 20 | + } |
| 21 | + |
| 22 | + const snippetItems: CompletionItem[] = []; |
| 23 | + for (const label of Object.keys(this.snippets)) { |
| 24 | + const snippetContent = this.snippets[label]; |
| 25 | + const snippetItem: CompletionItem = new CompletionItem(snippetContent.prefix); |
| 26 | + snippetItem.kind = CompletionItemKind.Snippet; |
| 27 | + snippetItem.detail = snippetContent.description; |
| 28 | + const insertText: string = (snippetContent.body as String[]).join('\n'); |
| 29 | + snippetItem.insertText = new SnippetString(insertText); |
| 30 | + snippetItem.documentation = beautifyDocument(insertText); |
| 31 | + snippetItems.push(snippetItem); |
| 32 | + } |
| 33 | + return snippetItems; |
| 34 | + } |
| 35 | + |
| 36 | + public setActivation(activation: boolean): void { |
| 37 | + this.activation = activation; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +export function beautifyDocument(raw: string): MarkdownString { |
| 42 | + const escapedString = raw.replace(/\$\{\d:?(.*?)\}/gm, '$1').replace(/\$\d/gm, ''); |
| 43 | + return new MarkdownString().appendCodeblock(escapedString, "java"); |
| 44 | +} |
0 commit comments