-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[plugin] basic support of snippets #3977
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
195 changes: 195 additions & 0 deletions
195
packages/monaco/src/browser/monaco-snippet-suggest-provider.ts
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,195 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2019 TypeFox and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
import * as jsoncparser from 'jsonc-parser'; | ||
import { injectable, inject } from 'inversify'; | ||
import URI from '@theia/core/lib/common/uri'; | ||
import { FileSystem, FileSystemError } from '@theia/filesystem/lib/common'; | ||
|
||
@injectable() | ||
export class MonacoSnippetSuggestProvider implements monaco.modes.ISuggestSupport { | ||
|
||
@inject(FileSystem) | ||
protected readonly filesystem: FileSystem; | ||
|
||
protected readonly snippets = new Map<string, MonacoSnippetSuggestion[]>(); | ||
protected readonly pendingSnippets = new Map<string, Promise<void>[]>(); | ||
|
||
async provideCompletionItems(model: monaco.editor.ITextModel): Promise<monaco.modes.ISuggestResult> { | ||
const languageId = model.getModeId(); // TODO: look up a language id at the position | ||
await this.loadSnippets(languageId); | ||
const suggestions = this.snippets.get(languageId) || []; | ||
return { suggestions }; | ||
} | ||
|
||
resolveCompletionItem(_: monaco.editor.ITextModel, __: monaco.Position, item: monaco.modes.ISuggestion): monaco.modes.ISuggestion { | ||
return item instanceof MonacoSnippetSuggestion ? item.resolve() : item; | ||
} | ||
|
||
protected async loadSnippets(scope: string): Promise<void> { | ||
const pending: Promise<void>[] = []; | ||
pending.push(...(this.pendingSnippets.get(scope) || [])); | ||
pending.push(...(this.pendingSnippets.get('*') || [])); | ||
if (pending.length) { | ||
await Promise.all(pending); | ||
} | ||
} | ||
|
||
fromURI(uri: string | URI, options: SnippetLoadOptions): Promise<void> { | ||
const pending = this.loadURI(uri, options); | ||
const { language } = options; | ||
const scopes = Array.isArray(language) ? language : !!language ? [language] : ['*']; | ||
for (const scope of scopes) { | ||
const pendingSnippets = this.pendingSnippets.get(scope) || []; | ||
pendingSnippets.push(pending); | ||
this.pendingSnippets.set(scope, pendingSnippets); | ||
} | ||
return pending; | ||
} | ||
/** | ||
* should NOT throw to prevent load erros on suggest | ||
*/ | ||
protected async loadURI(uri: string | URI, options: SnippetLoadOptions): Promise<void> { | ||
try { | ||
const { content } = await this.filesystem.resolveContent(uri.toString(), { encoding: 'utf-8' }); | ||
const snippets = content && jsoncparser.parse(content, undefined, { disallowComments: false }); | ||
this.fromJSON(snippets, options); | ||
} catch (e) { | ||
if (!FileSystemError.FileNotFound.is(e) && !FileSystemError.FileIsDirectory.is(e)) { | ||
console.error(e); | ||
} | ||
} | ||
} | ||
|
||
fromJSON(snippets: JsonSerializedSnippets | undefined, { language, source }: SnippetLoadOptions): void { | ||
this.parseSnippets(snippets, (name, snippet) => { | ||
let { prefix, body, description } = snippet; | ||
if (Array.isArray(body)) { | ||
body = body.join('\n'); | ||
} | ||
if (typeof prefix !== 'string' || typeof body !== 'string') { | ||
return; | ||
} | ||
const scopes: string[] = []; | ||
if (language) { | ||
if (Array.isArray(language)) { | ||
scopes.push(...language); | ||
} else { | ||
scopes.push(language); | ||
} | ||
} else if (typeof snippet.scope === 'string') { | ||
for (const rawScope of snippet.scope.split(',')) { | ||
const scope = rawScope.trim(); | ||
if (scope) { | ||
scopes.push(scope); | ||
} | ||
} | ||
} | ||
this.push({ | ||
scopes, | ||
name, | ||
prefix, | ||
description, | ||
body, | ||
source | ||
}); | ||
}); | ||
} | ||
protected parseSnippets(snippets: JsonSerializedSnippets | undefined, accept: (name: string, snippet: JsonSerializedSnippet) => void): void { | ||
if (typeof snippets === 'object') { | ||
// tslint:disable-next-line:forin | ||
for (const name in snippets) { | ||
const scopeOrTemplate = snippets[name]; | ||
if (JsonSerializedSnippet.is(scopeOrTemplate)) { | ||
accept(name, scopeOrTemplate); | ||
} else { | ||
this.parseSnippets(scopeOrTemplate, accept); | ||
} | ||
} | ||
} | ||
} | ||
|
||
push(...snippets: Snippet[]): void { | ||
for (const snippet of snippets) { | ||
for (const scope of snippet.scopes) { | ||
const languageSnippets = this.snippets.get(scope) || []; | ||
languageSnippets.push(new MonacoSnippetSuggestion(snippet)); | ||
this.snippets.set(scope, languageSnippets); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
export interface SnippetLoadOptions { | ||
language?: string | string[] | ||
source: string | ||
} | ||
|
||
export interface JsonSerializedSnippets { | ||
[name: string]: JsonSerializedSnippet | { [name: string]: JsonSerializedSnippet }; | ||
} | ||
export interface JsonSerializedSnippet { | ||
body: string | string[]; | ||
scope: string; | ||
prefix: string; | ||
description: string; | ||
} | ||
export namespace JsonSerializedSnippet { | ||
export function is(obj: Object | undefined): obj is JsonSerializedSnippet { | ||
return typeof obj === 'object' && 'body' in obj && 'prefix' in obj; | ||
} | ||
} | ||
|
||
export interface Snippet { | ||
readonly scopes: string[] | ||
readonly name: string | ||
readonly prefix: string | ||
readonly description: string | ||
readonly body: string | ||
readonly source: string | ||
} | ||
|
||
export class MonacoSnippetSuggestion implements monaco.modes.ISuggestion { | ||
|
||
readonly label: string; | ||
readonly detail: string; | ||
readonly sortText: string; | ||
readonly noAutoAccept = true; | ||
readonly type: 'snippet' = 'snippet'; | ||
readonly snippetType: 'textmate' = 'textmate'; | ||
|
||
insertText: string; | ||
documentation?: monaco.IMarkdownString; | ||
|
||
constructor(protected readonly snippet: Snippet) { | ||
this.label = snippet.prefix; | ||
this.detail = `${snippet.description || snippet.name} (${snippet.source})`; | ||
this.insertText = snippet.body; | ||
this.sortText = `z-${snippet.prefix}`; | ||
} | ||
|
||
protected resolved = false; | ||
resolve(): MonacoSnippetSuggestion { | ||
if (!this.resolved) { | ||
const codeSnippet = new monaco.snippetParser.SnippetParser().parse(this.snippet.body).toString(); | ||
this.insertText = codeSnippet; | ||
this.documentation = { value: '```\n' + codeSnippet + '```' }; | ||
this.resolved = true; | ||
} | ||
return this; | ||
} | ||
|
||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will it be committed ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found myself doing it over and over, and thought that we can specify relative plugins location for testing purposes.
Actually, i think maybe we should add a new example that includes only
plugin-ext-vscode
and its dependency, and exclude all irrelevant Theia extensions.// cc @svenefftinge