提交 02bbc149 编写于 作者: R Ramya Rao 提交者: GitHub

Option to trigger emmet expansion on tab (#32334)

上级 149cb6fa
...@@ -74,6 +74,11 @@ ...@@ -74,6 +74,11 @@
"type": "string", "type": "string",
"default": null, "default": null,
"description": "%emmetExtensionsPath%" "description": "%emmetExtensionsPath%"
},
"emmet.triggerExpansionOnTab": {
"type": "boolean",
"default": false,
"description": "%emmetTriggerExpansionOnTab%"
} }
} }
}, },
......
...@@ -28,5 +28,6 @@ ...@@ -28,5 +28,6 @@
"emmetShowExpandedAbbreviation": "Shows expanded emmet abbreviations as suggestions.\nThe option \"inMarkupAndStylesheetFilesOnly\" applies to html, haml, jade, slim, xml, xsl, css, scss, sass, less and stylus.\nThe option \"always\" applies to all parts of the file regardless of markup/css.", "emmetShowExpandedAbbreviation": "Shows expanded emmet abbreviations as suggestions.\nThe option \"inMarkupAndStylesheetFilesOnly\" applies to html, haml, jade, slim, xml, xsl, css, scss, sass, less and stylus.\nThe option \"always\" applies to all parts of the file regardless of markup/css.",
"emmetShowAbbreviationSuggestions": "Shows possible emmet abbreviations as suggestions. Not applicable in stylesheets or when emmet.showExpandedAbbreviation is set to \"never\".", "emmetShowAbbreviationSuggestions": "Shows possible emmet abbreviations as suggestions. Not applicable in stylesheets or when emmet.showExpandedAbbreviation is set to \"never\".",
"emmetIncludeLanguages": "Enable emmet abbreviations in languages that are not supported by default. Add a mapping here between the language and emmet supported language.\n Eg: {\"vue-html\": \"html\", \"javascript\": \"javascriptreact\"}", "emmetIncludeLanguages": "Enable emmet abbreviations in languages that are not supported by default. Add a mapping here between the language and emmet supported language.\n Eg: {\"vue-html\": \"html\", \"javascript\": \"javascriptreact\"}",
"emmetVariables": "Variables to be used in emmet snippets" "emmetVariables": "Variables to be used in emmet snippets",
"emmetTriggerExpansionOnTab": "When enabled, emmet abbreviations are expanded when pressing TAB."
} }
\ No newline at end of file
...@@ -82,17 +82,17 @@ export function wrapIndividualLinesWithAbbreviation(args) { ...@@ -82,17 +82,17 @@ export function wrapIndividualLinesWithAbbreviation(args) {
} }
export function expandEmmetAbbreviation(args) { export function expandEmmetAbbreviation(args): Thenable<boolean> {
const syntax = getSyntaxFromArgs(args); const syntax = getSyntaxFromArgs(args);
if (!syntax || !validate()) { if (!syntax || !validate()) {
return Promise.resolve(false); return fallbackTab();
} }
const editor = vscode.window.activeTextEditor; const editor = vscode.window.activeTextEditor;
let rootNode = parseDocument(editor.document); let rootNode = parseDocument(editor.document);
if (!rootNode) { if (!rootNode) {
return Promise.resolve(false); return fallbackTab();
} }
let abbreviationList: ExpandAbbreviationInput[] = []; let abbreviationList: ExpandAbbreviationInput[] = [];
...@@ -153,10 +153,18 @@ export function expandEmmetAbbreviation(args) { ...@@ -153,10 +153,18 @@ export function expandEmmetAbbreviation(args) {
abbreviationList.push({ syntax, abbreviation, rangeToReplace, filters }); abbreviationList.push({ syntax, abbreviation, rangeToReplace, filters });
}); });
return expandAbbreviationInRange(editor, abbreviationList, allAbbreviationsSame); return expandAbbreviationInRange(editor, abbreviationList, allAbbreviationsSame).then(success => {
if (!success) {
return fallbackTab();
}
});
} }
function fallbackTab(): Thenable<boolean> {
if (vscode.workspace.getConfiguration('emmet')['triggerExpansionOnTab'] === true) {
return vscode.commands.executeCommand('tab');
}
}
/** /**
* Checks if given position is a valid location to expand emmet abbreviation. * Checks if given position is a valid location to expand emmet abbreviation.
* Works only on html and css/less/scss syntax * Works only on html and css/less/scss syntax
...@@ -200,6 +208,7 @@ export function isValidLocationForEmmetAbbreviation(currentNode: Node, syntax: s ...@@ -200,6 +208,7 @@ export function isValidLocationForEmmetAbbreviation(currentNode: Node, syntax: s
* @param editor * @param editor
* @param expandAbbrList * @param expandAbbrList
* @param insertSameSnippet * @param insertSameSnippet
* @returns false if no snippet can be inserted.
*/ */
function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: ExpandAbbreviationInput[], insertSameSnippet: boolean): Thenable<boolean> { function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: ExpandAbbreviationInput[], insertSameSnippet: boolean): Thenable<boolean> {
if (!expandAbbrList || expandAbbrList.length === 0) { if (!expandAbbrList || expandAbbrList.length === 0) {
...@@ -217,6 +226,9 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex ...@@ -217,6 +226,9 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex
insertPromises.push(editor.insertSnippet(new vscode.SnippetString(expandedText), expandAbbrInput.rangeToReplace)); insertPromises.push(editor.insertSnippet(new vscode.SnippetString(expandedText), expandAbbrInput.rangeToReplace));
} }
}); });
if (insertPromises.length === 0) {
return Promise.resolve(false);
}
return Promise.all(insertPromises).then(() => Promise.resolve(true)); return Promise.all(insertPromises).then(() => Promise.resolve(true));
} }
......
...@@ -8,6 +8,8 @@ import nls = require('vs/nls'); ...@@ -8,6 +8,8 @@ import nls = require('vs/nls');
import { EmmetEditorAction } from 'vs/workbench/parts/emmet/electron-browser/emmetActions'; import { EmmetEditorAction } from 'vs/workbench/parts/emmet/electron-browser/emmetActions';
import { editorAction } from 'vs/editor/common/editorCommonExtensions'; import { editorAction } from 'vs/editor/common/editorCommonExtensions';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys'; import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { KeyCode } from 'vs/base/common/keyCodes';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
@editorAction @editorAction
class ExpandAbbreviationAction extends EmmetEditorAction { class ExpandAbbreviationAction extends EmmetEditorAction {
...@@ -18,7 +20,15 @@ class ExpandAbbreviationAction extends EmmetEditorAction { ...@@ -18,7 +20,15 @@ class ExpandAbbreviationAction extends EmmetEditorAction {
label: nls.localize('expandAbbreviationAction', "Emmet: Expand Abbreviation"), label: nls.localize('expandAbbreviationAction', "Emmet: Expand Abbreviation"),
alias: 'Emmet: Expand Abbreviation', alias: 'Emmet: Expand Abbreviation',
precondition: EditorContextKeys.writable, precondition: EditorContextKeys.writable,
actionName: 'expand_abbreviation' actionName: 'expand_abbreviation',
kbOpts: {
primary: KeyCode.Tab,
kbExpr: ContextKeyExpr.and(
EditorContextKeys.textFocus,
EditorContextKeys.tabDoesNotMoveFocus,
ContextKeyExpr.has('config.emmet.triggerExpansionOnTab')
)
}
}); });
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册