提交 0c50806e 编写于 作者: R Raymond Zhao

Refactor Emmet abbrevationAction files

上级 2c53d687
...@@ -66,7 +66,7 @@ function doWrapping(individualLines: boolean, args: any) { ...@@ -66,7 +66,7 @@ function doWrapping(individualLines: boolean, args: any) {
const helper = getEmmetHelper(); const helper = getEmmetHelper();
// Fetch general information for the succesive expansions. i.e. the ranges to replace and its contents // Fetch general information for the succesive expansions. i.e. the ranges to replace and its contents
let rangesToReplace: PreviewRangesWithContent[] = editor.selections.sort((a: vscode.Selection, b: vscode.Selection) => { return a.start.compareTo(b.start); }).map(selection => { const rangesToReplace: PreviewRangesWithContent[] = editor.selections.sort((a: vscode.Selection, b: vscode.Selection) => { return a.start.compareTo(b.start); }).map(selection => {
let rangeToReplace: vscode.Range = selection.isReversed ? new vscode.Range(selection.active, selection.anchor) : selection; let rangeToReplace: vscode.Range = selection.isReversed ? new vscode.Range(selection.active, selection.anchor) : selection;
if (!rangeToReplace.isSingleLine && rangeToReplace.end.character === 0) { if (!rangeToReplace.isSingleLine && rangeToReplace.end.character === 0) {
const previousLine = rangeToReplace.end.line - 1; const previousLine = rangeToReplace.end.line - 1;
...@@ -88,7 +88,7 @@ function doWrapping(individualLines: boolean, args: any) { ...@@ -88,7 +88,7 @@ function doWrapping(individualLines: boolean, args: any) {
rangeToReplace = new vscode.Range(rangeToReplace.start.line, rangeToReplace.start.character + extraWhitespaceSelected, rangeToReplace.end.line, rangeToReplace.end.character); rangeToReplace = new vscode.Range(rangeToReplace.start.line, rangeToReplace.start.character + extraWhitespaceSelected, rangeToReplace.end.line, rangeToReplace.end.character);
let textToWrapInPreview: string[]; let textToWrapInPreview: string[];
let textToReplace = editor.document.getText(rangeToReplace); const textToReplace = editor.document.getText(rangeToReplace);
if (individualLines) { if (individualLines) {
textToWrapInPreview = textToReplace.split('\n').map(x => x.trim()); textToWrapInPreview = textToReplace.split('\n').map(x => x.trim());
} else { } else {
...@@ -144,7 +144,7 @@ function doWrapping(individualLines: boolean, args: any) { ...@@ -144,7 +144,7 @@ function doWrapping(individualLines: boolean, args: any) {
const oldPreviewLines = oldPreviewRange.end.line - oldPreviewRange.start.line + 1; const oldPreviewLines = oldPreviewRange.end.line - oldPreviewRange.start.line + 1;
const newLinesInserted = expandedTextLines.length - oldPreviewLines; const newLinesInserted = expandedTextLines.length - oldPreviewLines;
let newPreviewLineStart = oldPreviewRange.start.line + totalLinesInserted; const newPreviewLineStart = oldPreviewRange.start.line + totalLinesInserted;
let newPreviewStart = oldPreviewRange.start.character; let newPreviewStart = oldPreviewRange.start.character;
const newPreviewLineEnd = oldPreviewRange.end.line + totalLinesInserted + newLinesInserted; const newPreviewLineEnd = oldPreviewRange.end.line + totalLinesInserted + newLinesInserted;
let newPreviewEnd = expandedTextLines[expandedTextLines.length - 1].length; let newPreviewEnd = expandedTextLines[expandedTextLines.length - 1].length;
...@@ -177,19 +177,19 @@ function doWrapping(individualLines: boolean, args: any) { ...@@ -177,19 +177,19 @@ function doWrapping(individualLines: boolean, args: any) {
return inPreview ? revertPreview().then(() => { return false; }) : Promise.resolve(inPreview); return inPreview ? revertPreview().then(() => { return false; }) : Promise.resolve(inPreview);
} }
let extractedResults = helper.extractAbbreviationFromText(inputAbbreviation); const extractedResults = helper.extractAbbreviationFromText(inputAbbreviation);
if (!extractedResults) { if (!extractedResults) {
return Promise.resolve(inPreview); return Promise.resolve(inPreview);
} else if (extractedResults.abbreviation !== inputAbbreviation) { } else if (extractedResults.abbreviation !== inputAbbreviation) {
// Not clear what should we do in this case. Warn the user? How? // Not clear what should we do in this case. Warn the user? How?
} }
let { abbreviation, filter } = extractedResults; const { abbreviation, filter } = extractedResults;
if (definitive) { if (definitive) {
const revertPromise = inPreview ? revertPreview() : Promise.resolve(); const revertPromise = inPreview ? revertPreview() : Promise.resolve();
return revertPromise.then(() => { return revertPromise.then(() => {
const expandAbbrList: ExpandAbbreviationInput[] = rangesToReplace.map(rangesAndContent => { const expandAbbrList: ExpandAbbreviationInput[] = rangesToReplace.map(rangesAndContent => {
let rangeToReplace = rangesAndContent.originalRange; const rangeToReplace = rangesAndContent.originalRange;
let textToWrap: string[]; let textToWrap: string[];
if (individualLines) { if (individualLines) {
textToWrap = rangesAndContent.textToWrapInPreview; textToWrap = rangesAndContent.textToWrapInPreview;
...@@ -270,17 +270,17 @@ export function expandEmmetAbbreviation(args: any): Thenable<boolean | undefined ...@@ -270,17 +270,17 @@ export function expandEmmetAbbreviation(args: any): Thenable<boolean | undefined
return fallbackTab(); return fallbackTab();
} }
let abbreviationList: ExpandAbbreviationInput[] = []; const abbreviationList: ExpandAbbreviationInput[] = [];
let firstAbbreviation: string; let firstAbbreviation: string;
let allAbbreviationsSame: boolean = true; let allAbbreviationsSame: boolean = true;
const helper = getEmmetHelper(); const helper = getEmmetHelper();
let getAbbreviation = (document: vscode.TextDocument, selection: vscode.Selection, position: vscode.Position, syntax: string): [vscode.Range | null, string, string] => { const getAbbreviation = (document: vscode.TextDocument, selection: vscode.Selection, position: vscode.Position, syntax: string): [vscode.Range | null, string, string] => {
position = document.validatePosition(position); position = document.validatePosition(position);
let rangeToReplace: vscode.Range = selection; let rangeToReplace: vscode.Range = selection;
let abbr = document.getText(rangeToReplace); let abbr = document.getText(rangeToReplace);
if (!rangeToReplace.isEmpty) { if (!rangeToReplace.isEmpty) {
let extractedResults = helper.extractAbbreviationFromText(abbr); const extractedResults = helper.extractAbbreviationFromText(abbr);
if (extractedResults) { if (extractedResults) {
return [rangeToReplace, extractedResults.abbreviation, extractedResults.filter]; return [rangeToReplace, extractedResults.abbreviation, extractedResults.filter];
} }
...@@ -293,23 +293,23 @@ export function expandEmmetAbbreviation(args: any): Thenable<boolean | undefined ...@@ -293,23 +293,23 @@ export function expandEmmetAbbreviation(args: any): Thenable<boolean | undefined
// Expand cases like <div to <div></div> explicitly // Expand cases like <div to <div></div> explicitly
// else we will end up with <<div></div> // else we will end up with <<div></div>
if (syntax === 'html') { if (syntax === 'html') {
let matches = textTillPosition.match(/<(\w+)$/); const matches = textTillPosition.match(/<(\w+)$/);
if (matches) { if (matches) {
abbr = matches[1]; abbr = matches[1];
rangeToReplace = new vscode.Range(position.translate(0, -(abbr.length + 1)), position); rangeToReplace = new vscode.Range(position.translate(0, -(abbr.length + 1)), position);
return [rangeToReplace, abbr, '']; return [rangeToReplace, abbr, ''];
} }
} }
let extractedResults = helper.extractAbbreviation(toLSTextDocument(editor.document), position, false); const extractedResults = helper.extractAbbreviation(toLSTextDocument(editor.document), position, false);
if (!extractedResults) { if (!extractedResults) {
return [null, '', '']; return [null, '', ''];
} }
let { abbreviationRange, abbreviation, filter } = extractedResults; const { abbreviationRange, abbreviation, filter } = extractedResults;
return [new vscode.Range(abbreviationRange.start.line, abbreviationRange.start.character, abbreviationRange.end.line, abbreviationRange.end.character), abbreviation, filter]; return [new vscode.Range(abbreviationRange.start.line, abbreviationRange.start.character, abbreviationRange.end.line, abbreviationRange.end.character), abbreviation, filter];
}; };
let selectionsInReverseOrder = editor.selections.slice(0); const selectionsInReverseOrder = editor.selections.slice(0);
selectionsInReverseOrder.sort((a, b) => { selectionsInReverseOrder.sort((a, b) => {
const posA = a.isReversed ? a.anchor : a.active; const posA = a.isReversed ? a.anchor : a.active;
const posB = b.isReversed ? b.anchor : b.active; const posB = b.isReversed ? b.anchor : b.active;
...@@ -322,7 +322,7 @@ export function expandEmmetAbbreviation(args: any): Thenable<boolean | undefined ...@@ -322,7 +322,7 @@ export function expandEmmetAbbreviation(args: any): Thenable<boolean | undefined
return rootNode; return rootNode;
} }
let usePartialParsing = vscode.workspace.getConfiguration('emmet')['optimizeStylesheetParsing'] === true; const usePartialParsing = vscode.workspace.getConfiguration('emmet')['optimizeStylesheetParsing'] === true;
if (editor.selections.length === 1 && isStyleSheet(editor.document.languageId) && usePartialParsing && editor.document.lineCount > 1000) { if (editor.selections.length === 1 && isStyleSheet(editor.document.languageId) && usePartialParsing && editor.document.lineCount > 1000) {
rootNode = parsePartialStylesheet(editor.document, editor.selection.isReversed ? editor.selection.anchor : editor.selection.active); rootNode = parsePartialStylesheet(editor.document, editor.selection.isReversed ? editor.selection.anchor : editor.selection.active);
} else { } else {
...@@ -333,8 +333,8 @@ export function expandEmmetAbbreviation(args: any): Thenable<boolean | undefined ...@@ -333,8 +333,8 @@ export function expandEmmetAbbreviation(args: any): Thenable<boolean | undefined
} }
selectionsInReverseOrder.forEach(selection => { selectionsInReverseOrder.forEach(selection => {
let position = selection.isReversed ? selection.anchor : selection.active; const position = selection.isReversed ? selection.anchor : selection.active;
let [rangeToReplace, abbreviation, filter] = getAbbreviation(editor.document, selection, position, syntax); const [rangeToReplace, abbreviation, filter] = getAbbreviation(editor.document, selection, position, syntax);
if (!rangeToReplace) { if (!rangeToReplace) {
return; return;
} }
...@@ -578,7 +578,7 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex ...@@ -578,7 +578,7 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex
// Snippet to replace at multiple cursors are not the same // Snippet to replace at multiple cursors are not the same
// `editor.insertSnippet` will have to be called for each instance separately // `editor.insertSnippet` will have to be called for each instance separately
// We will not be able to maintain multiple cursors after snippet insertion // We will not be able to maintain multiple cursors after snippet insertion
let insertPromises: Thenable<boolean>[] = []; const insertPromises: Thenable<boolean>[] = [];
if (!insertSameSnippet) { if (!insertSameSnippet) {
expandAbbrList.sort((a: ExpandAbbreviationInput, b: ExpandAbbreviationInput) => { return b.rangeToReplace.start.compareTo(a.rangeToReplace.start); }).forEach((expandAbbrInput: ExpandAbbreviationInput) => { expandAbbrList.sort((a: ExpandAbbreviationInput, b: ExpandAbbreviationInput) => { return b.rangeToReplace.start.compareTo(a.rangeToReplace.start); }).forEach((expandAbbrInput: ExpandAbbreviationInput) => {
let expandedText = expandAbbr(expandAbbrInput); let expandedText = expandAbbr(expandAbbrInput);
...@@ -596,8 +596,8 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex ...@@ -596,8 +596,8 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex
// 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); const expandedText = expandAbbr(anyExpandAbbrInput);
let allRanges = expandAbbrList.map(value => { const allRanges = expandAbbrList.map(value => {
return new vscode.Range(value.rangeToReplace.start.line, value.rangeToReplace.start.character, value.rangeToReplace.end.line, value.rangeToReplace.end.character); return new vscode.Range(value.rangeToReplace.start.line, value.rangeToReplace.start.character, value.rangeToReplace.end.line, value.rangeToReplace.end.character);
}); });
if (expandedText) { if (expandedText) {
...@@ -614,7 +614,7 @@ function walk(root: any, fn: ((node: any) => boolean)): boolean { ...@@ -614,7 +614,7 @@ function walk(root: any, fn: ((node: any) => boolean)): boolean {
let ctx = root; let ctx = root;
while (ctx) { while (ctx) {
let next = ctx.next; const next = ctx.next;
if (fn(ctx) === false || walk(ctx.firstChild, fn) === false) { if (fn(ctx) === false || walk(ctx.firstChild, fn) === false) {
return false; return false;
} }
...@@ -653,7 +653,7 @@ function expandAbbr(input: ExpandAbbreviationInput): string | undefined { ...@@ -653,7 +653,7 @@ function expandAbbr(input: ExpandAbbreviationInput): string | undefined {
// Expand the abbreviation // Expand the abbreviation
if (input.textToWrap) { if (input.textToWrap) {
let parsedAbbr = helper.parseAbbreviation(input.abbreviation, expandOptions); const parsedAbbr = helper.parseAbbreviation(input.abbreviation, expandOptions);
if (input.rangeToReplace.isSingleLine && input.textToWrap.length === 1) { if (input.rangeToReplace.isSingleLine && input.textToWrap.length === 1) {
// Fetch rightmost element in the parsed abbreviation (i.e the element that will contain the wrapped text). // Fetch rightmost element in the parsed abbreviation (i.e the element that will contain the wrapped text).
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册