提交 868b8208 编写于 作者: R Ramya Achutha Rao

Pass syntax from core to Wrap Abbr just like Expand Abbr

上级 7eda8927
...@@ -8,7 +8,7 @@ import { expand } from '@emmetio/expand-abbreviation'; ...@@ -8,7 +8,7 @@ import { expand } from '@emmetio/expand-abbreviation';
import parseStylesheet from '@emmetio/css-parser'; import parseStylesheet from '@emmetio/css-parser';
import parse from '@emmetio/html-matcher'; import parse from '@emmetio/html-matcher';
import Node from '@emmetio/node'; import Node from '@emmetio/node';
import { getSyntax, getNode, getInnerRange } from './util'; import { getNode, getInnerRange } from './util';
import { getExpandOptions, extractAbbreviation, isStyleSheet, isAbbreviationValid } from 'vscode-emmet-helper'; import { getExpandOptions, extractAbbreviation, isStyleSheet, isAbbreviationValid } from 'vscode-emmet-helper';
import { DocumentStreamReader } from './bufferStream'; import { DocumentStreamReader } from './bufferStream';
...@@ -19,14 +19,14 @@ interface ExpandAbbreviationInput { ...@@ -19,14 +19,14 @@ interface ExpandAbbreviationInput {
textToWrap?: string; textToWrap?: string;
} }
export function wrapWithAbbreviation() { export function wrapWithAbbreviation(args) {
let editor = vscode.window.activeTextEditor; const syntax = getSyntaxFromArgs(args);
if (!editor) { if (!syntax) {
vscode.window.showInformationMessage('No editor is active');
return; return;
} }
const editor = vscode.window.activeTextEditor;
const newLine = editor.document.eol === vscode.EndOfLine.LF ? '\n' : '\r\n'; const newLine = editor.document.eol === vscode.EndOfLine.LF ? '\n' : '\r\n';
let syntax = getSyntax(editor.document);
vscode.window.showInputBox({ prompt: 'Enter Abbreviation' }).then(abbreviation => { vscode.window.showInputBox({ prompt: 'Enter Abbreviation' }).then(abbreviation => {
if (!abbreviation || !abbreviation.trim() || !isAbbreviationValid(syntax, abbreviation)) { return; } if (!abbreviation || !abbreviation.trim() || !isAbbreviationValid(syntax, abbreviation)) { return; }
...@@ -69,16 +69,13 @@ export function wrapWithAbbreviation() { ...@@ -69,16 +69,13 @@ export function wrapWithAbbreviation() {
} }
export function expandAbbreviation(args) { export function expandAbbreviation(args) {
const syntax = getSyntaxFromArgs(args);
let editor = vscode.window.activeTextEditor; if (!syntax) {
if (!editor) {
vscode.window.showInformationMessage('No editor is active');
return; return;
} }
if (typeof args !== 'object' || !args['syntax']) {
return; const editor = vscode.window.activeTextEditor;
}
let syntax = args['syntax'];
let parseContent = isStyleSheet(syntax) ? parseStylesheet : parse; let parseContent = isStyleSheet(syntax) ? parseStylesheet : parse;
let rootNode: Node = parseContent(new DocumentStreamReader(editor.document)); let rootNode: Node = parseContent(new DocumentStreamReader(editor.document));
...@@ -141,11 +138,11 @@ export function isValidLocationForEmmetAbbreviation(currentNode: Node, syntax: s ...@@ -141,11 +138,11 @@ export function isValidLocationForEmmetAbbreviation(currentNode: Node, syntax: s
/** /**
* Expands abbreviations as detailed in expandAbbrList in the editor * Expands abbreviations as detailed in expandAbbrList in the editor
* @param editor * @param editor
* @param expandAbbrList * @param expandAbbrList
* @param syntax * @param syntax
* @param insertSameSnippet * @param insertSameSnippet
* @param preceedingWhiteSpace * @param preceedingWhiteSpace
*/ */
function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: ExpandAbbreviationInput[], syntax: string, insertSameSnippet: boolean, preceedingWhiteSpace: string = '') { function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: ExpandAbbreviationInput[], syntax: string, insertSameSnippet: boolean, preceedingWhiteSpace: string = '') {
if (!expandAbbrList || expandAbbrList.length === 0) { if (!expandAbbrList || expandAbbrList.length === 0) {
...@@ -167,7 +164,7 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex ...@@ -167,7 +164,7 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex
} }
// Snippet to replace at all cursors are the same // Snippet to replace at all cursors are the same
// We can pass all ranges to `editor.insertSnippet` in a single call so that // We can pass all ranges to `editor.insertSnippet` in a single call so that
// all cursors are maintained after snippet insertion // all cursors are maintained after snippet insertion
const anyExpandAbbrInput = expandAbbrList[0]; const anyExpandAbbrInput = expandAbbrList[0];
let expandedText = expandAbbr(anyExpandAbbrInput, preceedingWhiteSpace, newLine); let expandedText = expandAbbr(anyExpandAbbrInput, preceedingWhiteSpace, newLine);
...@@ -180,7 +177,7 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex ...@@ -180,7 +177,7 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex
} }
/** /**
* Expands abbreviation as detailed in given input. * Expands abbreviation as detailed in given input.
* If there is textToWrap, then given preceedingWhiteSpace is applied * If there is textToWrap, then given preceedingWhiteSpace is applied
*/ */
function expandAbbr(input: ExpandAbbreviationInput, preceedingWhiteSpace: string, newLine: string): string { function expandAbbr(input: ExpandAbbreviationInput, preceedingWhiteSpace: string, newLine: string): string {
...@@ -195,4 +192,17 @@ function expandAbbr(input: ExpandAbbreviationInput, preceedingWhiteSpace: string ...@@ -195,4 +192,17 @@ function expandAbbr(input: ExpandAbbreviationInput, preceedingWhiteSpace: string
} }
return expandedText.split(newLine).map(line => preceedingWhiteSpace + line).join(newLine); return expandedText.split(newLine).map(line => preceedingWhiteSpace + line).join(newLine);
}
function getSyntaxFromArgs(args: any): string {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active.');
return;
}
if (typeof args !== 'object' || !args['syntax']) {
vscode.window.showInformationMessage('Cannot resolve language at cursor.');
return;
}
return args['syntax'];
} }
\ No newline at end of file
...@@ -31,8 +31,8 @@ export function activate(context: vscode.ExtensionContext) { ...@@ -31,8 +31,8 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(provider); context.subscriptions.push(provider);
}); });
context.subscriptions.push(vscode.commands.registerCommand('emmet.wrapWithAbbreviation', () => { context.subscriptions.push(vscode.commands.registerCommand('emmet.wrapWithAbbreviation', (args) => {
wrapWithAbbreviation(); wrapWithAbbreviation(args);
})); }));
context.subscriptions.push(vscode.commands.registerCommand('emmet.expandAbbreviation', (args) => { context.subscriptions.push(vscode.commands.registerCommand('emmet.expandAbbreviation', (args) => {
......
...@@ -297,7 +297,7 @@ export abstract class EmmetEditorAction extends EditorAction { ...@@ -297,7 +297,7 @@ export abstract class EmmetEditorAction extends EditorAction {
const commandService = accessor.get(ICommandService); const commandService = accessor.get(ICommandService);
let mappedCommand = configurationService.getConfiguration<IEmmetConfiguration>().emmet.useNewEmmet ? this.actionMap[this.id] : undefined; let mappedCommand = configurationService.getConfiguration<IEmmetConfiguration>().emmet.useNewEmmet ? this.actionMap[this.id] : undefined;
if (mappedCommand && mappedCommand !== 'emmet.expandAbbreviation') { if (mappedCommand && mappedCommand !== 'emmet.expandAbbreviation' && mappedCommand !== 'emmet.wrapWithAbbreviation') {
return commandService.executeCommand<void>(mappedCommand); return commandService.executeCommand<void>(mappedCommand);
} }
...@@ -312,7 +312,7 @@ export abstract class EmmetEditorAction extends EditorAction { ...@@ -312,7 +312,7 @@ export abstract class EmmetEditorAction extends EditorAction {
this.emmetActionName this.emmetActionName
); );
if (mappedCommand === 'emmet.expandAbbreviation') { if (mappedCommand === 'emmet.expandAbbreviation' || mappedCommand === 'emmet.wrapWithAbbreviation') {
let syntax = editorAccessor.getSyntax(); let syntax = editorAccessor.getSyntax();
return commandService.executeCommand<void>(mappedCommand, { syntax }); return commandService.executeCommand<void>(mappedCommand, { syntax });
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册