-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
joh/innocent tiger #155321
Merged
Merged
joh/innocent tiger #155321
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0fd8cd8
surround with code action needs non-empty selection, uses active end …
jrieken b8e6d89
no hidden snippets as code action
jrieken 36846c1
add concept of top level snippet and allow to get snippets without la…
jrieken 1be8606
make snippet-contribution a proper contrib-file, remove local registr…
jrieken 7fd96db
tweak label
jrieken 92329e4
add "start with snippet" to empty editor text, change things to use f…
jrieken 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
29 changes: 29 additions & 0 deletions
29
src/vs/workbench/contrib/snippets/browser/commands/abstractSnippetsActions.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,29 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { EditorAction2 } from 'vs/editor/browser/editorExtensions'; | ||
import { localize } from 'vs/nls'; | ||
import { Action2, IAction2Options } from 'vs/platform/actions/common/actions'; | ||
|
||
const defaultOptions: Partial<IAction2Options> = { | ||
category: { | ||
value: localize('snippets', 'Snippets'), | ||
original: 'Snippets' | ||
}, | ||
}; | ||
|
||
export abstract class SnippetsAction extends Action2 { | ||
|
||
constructor(desc: Readonly<IAction2Options>) { | ||
super({ ...defaultOptions, ...desc }); | ||
} | ||
} | ||
|
||
export abstract class SnippetEditorAction extends EditorAction2 { | ||
|
||
constructor(desc: Readonly<IAction2Options>) { | ||
super({ ...defaultOptions, ...desc }); | ||
} | ||
} |
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
116 changes: 116 additions & 0 deletions
116
src/vs/workbench/contrib/snippets/browser/commands/emptyFileSnippets.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,116 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { groupBy, isFalsyOrEmpty } from 'vs/base/common/arrays'; | ||
import { compare } from 'vs/base/common/strings'; | ||
import { getCodeEditor } from 'vs/editor/browser/editorBrowser'; | ||
import { ILanguageService } from 'vs/editor/common/languages/language'; | ||
import { SnippetController2 } from 'vs/editor/contrib/snippet/browser/snippetController2'; | ||
import { localize } from 'vs/nls'; | ||
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; | ||
import { IQuickInputService, IQuickPickItem, IQuickPickSeparator } from 'vs/platform/quickinput/common/quickInput'; | ||
import { SnippetsAction } from 'vs/workbench/contrib/snippets/browser/commands/abstractSnippetsActions'; | ||
import { ISnippetsService } from 'vs/workbench/contrib/snippets/browser/snippets'; | ||
import { Snippet } from 'vs/workbench/contrib/snippets/browser/snippetsFile'; | ||
import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; | ||
|
||
export class SelectSnippetForEmptyFile extends SnippetsAction { | ||
|
||
static readonly Id = 'workbench.action.populateFromSnippet'; | ||
|
||
constructor() { | ||
super({ | ||
id: SelectSnippetForEmptyFile.Id, | ||
title: { | ||
value: localize('label', 'Populate from Snippet'), | ||
original: 'Populate from Snippet' | ||
}, | ||
f1: true, | ||
}); | ||
} | ||
|
||
async run(accessor: ServicesAccessor): Promise<void> { | ||
const snippetService = accessor.get(ISnippetsService); | ||
const quickInputService = accessor.get(IQuickInputService); | ||
const editorService = accessor.get(IEditorService); | ||
const langService = accessor.get(ILanguageService); | ||
|
||
const editor = getCodeEditor(editorService.activeTextEditorControl); | ||
if (!editor || !editor.hasModel()) { | ||
return; | ||
} | ||
|
||
const snippets = await snippetService.getSnippets(undefined, { topLevelSnippets: true, noRecencySort: true, includeNoPrefixSnippets: true }); | ||
if (snippets.length === 0) { | ||
return; | ||
} | ||
|
||
const selection = await this._pick(quickInputService, langService, snippets); | ||
if (!selection) { | ||
return; | ||
} | ||
|
||
if (editor.hasModel()) { | ||
// apply snippet edit -> replaces everything | ||
SnippetController2.get(editor)?.apply([{ | ||
range: editor.getModel().getFullModelRange(), | ||
template: selection.snippet.body | ||
}]); | ||
|
||
// set language if possible | ||
if (langService.isRegisteredLanguageId(selection.langId)) { | ||
editor.getModel().setMode(selection.langId); | ||
} | ||
} | ||
} | ||
|
||
private async _pick(quickInputService: IQuickInputService, langService: ILanguageService, snippets: Snippet[]) { | ||
|
||
// spread snippet onto each language it supports | ||
type SnippetAndLanguage = { langId: string; snippet: Snippet }; | ||
const all: SnippetAndLanguage[] = []; | ||
for (const snippet of snippets) { | ||
if (isFalsyOrEmpty(snippet.scopes)) { | ||
all.push({ langId: '', snippet }); | ||
} else { | ||
for (const langId of snippet.scopes) { | ||
all.push({ langId, snippet }); | ||
} | ||
} | ||
} | ||
|
||
type SnippetAndLanguagePick = IQuickPickItem & { snippet: SnippetAndLanguage }; | ||
const picks: (SnippetAndLanguagePick | IQuickPickSeparator)[] = []; | ||
|
||
const groups = groupBy(all, (a, b) => compare(a.langId, b.langId)); | ||
|
||
for (const group of groups) { | ||
let first = true; | ||
for (const item of group) { | ||
|
||
if (first) { | ||
picks.push({ | ||
type: 'separator', | ||
label: langService.getLanguageName(item.langId) ?? item.langId | ||
}); | ||
first = false; | ||
} | ||
|
||
picks.push({ | ||
snippet: item, | ||
label: item.snippet.prefix || item.snippet.name, | ||
detail: item.snippet.description | ||
}); | ||
} | ||
} | ||
|
||
const pick = await quickInputService.pick(picks, { | ||
placeHolder: localize('placeholder', 'Select a snippet'), | ||
matchOnDetail: true, | ||
}); | ||
|
||
return pick?.snippet; | ||
} | ||
} |
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.
getElementsByTagName
is slightly faster