-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable client snippet before the language server is ready (#1101)
Signed-off-by: Sheng Chen <[email protected]>
- Loading branch information
Showing
6 changed files
with
204 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
{ | ||
"sysout": { | ||
"prefix": "sysout", | ||
"body": [ | ||
"System.out.println($0);" | ||
], | ||
"description": "Print to standard out" | ||
}, | ||
"syserr": { | ||
"prefix": "syserr", | ||
"body": [ | ||
"System.err.println($0);" | ||
], | ||
"description": "Print to standard err" | ||
}, | ||
"fori": { | ||
"prefix": "fori", | ||
"body": [ | ||
"for (${1:int} ${2:i} = ${3:0}; ${2:i} < ${4:max}; ${2:i}++) {", | ||
"\t$0", | ||
"}" | ||
], | ||
"description": "Indexed for loop" | ||
}, | ||
"foreach": { | ||
"prefix": "foreach", | ||
"body": [ | ||
"for (${1:type} ${2:var} : ${3:iterable}) {", | ||
"\t$0", | ||
"}" | ||
], | ||
"description": "Enhanced for loop" | ||
}, | ||
"if": { | ||
"prefix": "if", | ||
"body": [ | ||
"if (${1:condition}) {", | ||
"\t$0", | ||
"}" | ||
], | ||
"description": "if statement" | ||
}, | ||
"ifelse": { | ||
"prefix": "ifelse", | ||
"body": [ | ||
"if (${1:condition}) {", | ||
"\t$2", | ||
"} else {", | ||
"\t$0", | ||
"}" | ||
], | ||
"description": "if/else statement" | ||
}, | ||
"ifnull": { | ||
"prefix": "ifnull", | ||
"body": [ | ||
"if (${1:condition} == null) {", | ||
"\t$0", | ||
"}" | ||
], | ||
"description": "if statement checking for null" | ||
}, | ||
"ifnotnull": { | ||
"prefix": "ifnotnull", | ||
"body": [ | ||
"if (${1:condition} != null) {", | ||
"\t$0", | ||
"}" | ||
], | ||
"description": "if statement checking for not null" | ||
}, | ||
"While Statement": { | ||
"prefix": "while", | ||
"body": [ | ||
"while (${1:condition}) {", | ||
"\t$0", | ||
"}" | ||
], | ||
"description": "While Statement" | ||
}, | ||
"Do-While Statement": { | ||
"prefix": "dowhile", | ||
"body": [ | ||
"do {", | ||
"\t$0", | ||
"} while (${1:condition});" | ||
], | ||
"description": "Do-While Statement" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
|
||
import { CompletionItemProvider, TextDocument, Position, CancellationToken, CompletionContext, CompletionItem, CompletionItemKind, SnippetString, MarkdownString } from "vscode"; | ||
import * as fse from 'fs-extra'; | ||
import * as path from 'path'; | ||
|
||
export class SnippetCompletionProvider implements CompletionItemProvider { | ||
|
||
private snippets: {}; | ||
private activation: boolean; | ||
|
||
public constructor() { | ||
this.activation = true; | ||
this.snippets = fse.readJSONSync(path.join(__dirname, '..', 'snippets', 'server.json')); | ||
} | ||
|
||
public async provideCompletionItems(_document: TextDocument, _position: Position, _token: CancellationToken, _context: CompletionContext): Promise<CompletionItem[]> { | ||
if (!this.activation) { | ||
return []; | ||
} | ||
|
||
const snippetItems: CompletionItem[] = []; | ||
for (const label of Object.keys(this.snippets)) { | ||
const snippetContent = this.snippets[label]; | ||
const snippetItem: CompletionItem = new CompletionItem(snippetContent.prefix); | ||
snippetItem.kind = CompletionItemKind.Snippet; | ||
snippetItem.detail = snippetContent.description; | ||
const insertText: string = (snippetContent.body as String[]).join('\n'); | ||
snippetItem.insertText = new SnippetString(insertText); | ||
snippetItem.documentation = beautifyDocument(insertText); | ||
snippetItems.push(snippetItem); | ||
} | ||
return snippetItems; | ||
} | ||
|
||
public setActivation(activation: boolean): void { | ||
this.activation = activation; | ||
} | ||
} | ||
|
||
export function beautifyDocument(raw: string): MarkdownString { | ||
const escapedString = raw.replace(/\$\{\d:?(.*?)\}/gm, '$1').replace(/\$\d/gm, ''); | ||
return new MarkdownString().appendCodeblock(escapedString, "java"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as assert from 'assert'; | ||
import { beautifyDocument } from '../src/snippetCompletionProvider'; | ||
import { MarkdownString } from 'vscode'; | ||
|
||
suite('Snippet Completion Provider', () => { | ||
|
||
test('should render document correctly', () => { | ||
// tslint:disable: prefer-template | ||
const raw = "for (${1:int} ${2:i} = ${3:0}; ${2:i} < ${4:max}; ${2:i}++) {\n" + "\t$0\n" + "}"; | ||
const markdownString: MarkdownString = beautifyDocument(raw); | ||
const expected: string = "\n```java\n" + "for (int i = 0; i < max; i++) {\n" + "\t\n" + "}\n" + "```\n"; | ||
assert.equal(markdownString.value, expected); | ||
}); | ||
}); |