From a057a5974a34c13eedf3db1afdc12daefe3b6f2e Mon Sep 17 00:00:00 2001 From: Ramya Achutha Rao Date: Wed, 21 Jun 2017 14:08:42 -0700 Subject: [PATCH] Prepping to move completion provider to a re-usable module --- extensions/emmet/src/abbreviationActions.ts | 108 ++-------- extensions/emmet/src/balance.ts | 4 +- .../emmet/src/defaultCompletionProvider.ts | 64 ++++++ .../src/{ => emmetForVSCode}/bufferStream.ts | 0 .../emmetCompletionProvider.ts | 73 +++---- .../emmet/src/emmetForVSCode/emmetUtils.ts | 200 ++++++++++++++++++ extensions/emmet/src/extension.ts | 11 +- extensions/emmet/src/matchTag.ts | 4 +- extensions/emmet/src/mergeLines.ts | 4 +- extensions/emmet/src/selectItem.ts | 6 +- extensions/emmet/src/selectItemHTML.ts | 4 +- extensions/emmet/src/selectItemStylesheet.ts | 4 +- extensions/emmet/src/splitJoinTag.ts | 4 +- extensions/emmet/src/toggleComment.ts | 5 +- extensions/emmet/src/updateTag.ts | 4 +- extensions/emmet/src/util.ts | 140 +----------- 16 files changed, 349 insertions(+), 286 deletions(-) create mode 100644 extensions/emmet/src/defaultCompletionProvider.ts rename extensions/emmet/src/{ => emmetForVSCode}/bufferStream.ts (100%) rename extensions/emmet/src/{ => emmetForVSCode}/emmetCompletionProvider.ts (59%) create mode 100644 extensions/emmet/src/emmetForVSCode/emmetUtils.ts diff --git a/extensions/emmet/src/abbreviationActions.ts b/extensions/emmet/src/abbreviationActions.ts index 79592cc0e64..5b1639ab9a7 100644 --- a/extensions/emmet/src/abbreviationActions.ts +++ b/extensions/emmet/src/abbreviationActions.ts @@ -5,15 +5,12 @@ import * as vscode from 'vscode'; import { expand } from '@emmetio/expand-abbreviation'; -import * as extract from '@emmetio/extract-abbreviation'; import parseStylesheet from '@emmetio/css-parser'; import parse from '@emmetio/html-matcher'; import Node from '@emmetio/node'; - -import { getSyntax, getProfile, getVariables, isStyleSheet, getNode, getInnerRange } from './util'; -import { DocumentStreamReader } from './bufferStream'; - -const field = (index, placeholder) => `\${${index}${placeholder ? ':' + placeholder : ''}}`; +import { getSyntax } from './util'; +import { getExpandOptions, extractAbbreviation, isStyleSheet, getNode, getInnerRange } from './emmetForVSCode/emmetUtils'; +import { DocumentStreamReader } from './emmetForVSCode/bufferStream'; export function wrapWithAbbreviation() { let editor = vscode.window.activeTextEditor; @@ -45,89 +42,37 @@ export function expandAbbreviation(args) { if (typeof args !== 'object' || !args['syntax']) { return; } - let output = expandAbbreviationHelper(args['syntax'], editor.document, editor.selection); - if (output) { - editor.insertSnippet(new vscode.SnippetString(output.expandedText), output.abbreviationRange); - } -} - -export interface ExpandAbbreviationHelperOutput { - expandedText: string; - abbreviationRange: vscode.Range; - abbreviation: string; - syntax: string; -} - -/** - * Expands abbreviation at given range in the given document - * @param syntax string syntax to be used for expanding abbreviations - * @param document vscode.TextDocument - * @param abbreviationRange vscode.Range range of the abbreviation that needs to be expanded - * */ -export function expandAbbreviationHelper(syntax: string, document: vscode.TextDocument, abbreviationRange: vscode.Range): ExpandAbbreviationHelperOutput { - if (!syntax) { - return; - } - let abbreviation = document.getText(abbreviationRange); + let syntax = args['syntax']; + let abbreviationRange: vscode.Range = editor.selection; + let position = editor.selection.isReversed ? editor.selection.anchor : editor.selection.active; + let abbreviation = editor.document.getText(abbreviationRange); if (abbreviationRange.isEmpty) { - [abbreviationRange, abbreviation] = extractAbbreviation(document, abbreviationRange.start); + [abbreviationRange, abbreviation] = extractAbbreviation(editor.document, position); + } + + let parseContent = isStyleSheet(syntax) ? parseStylesheet : parse; + let rootNode: Node = parseContent(new DocumentStreamReader(editor.document)); + let currentNode = getNode(rootNode, position); + + if (!isValidLocationForEmmetAbbreviation(currentNode, syntax, position)) { + return; } let expandedText = expand(abbreviation, getExpandOptions(syntax)); - return { expandedText, abbreviationRange, abbreviation, syntax }; -} - -/** - * Checks whether given position is valid for emmet abbreviation and returns appropriate syntax - * @param syntax string language mode of current document - * @param document vscode.Textdocument - * @param position vscode.Position position of the abbreviation that needs to be expanded - */ -export function syntaxHelper(syntax: string, document: vscode.TextDocument, position: vscode.Position): string { - let parseContent = isStyleSheet(syntax) ? parseStylesheet : parse; - let rootNode: Node = parseContent(new DocumentStreamReader(document)); - let currentNode = getNode(rootNode, position); - - if (forceCssSyntax(syntax, currentNode, position)) { - return 'css'; - } else if (!isValidLocationForEmmetAbbreviation(currentNode, syntax, position)) { - return; + if (expandedText) { + editor.insertSnippet(new vscode.SnippetString(expandedText), abbreviationRange); } - return syntax; } -/** - * Extracts abbreviation from the given position in the given document - */ -function extractAbbreviation(document: vscode.TextDocument, position: vscode.Position): [vscode.Range, string] { - let currentLine = document.lineAt(position.line).text; - let result = extract(currentLine, position.character, true); - if (!result) { - return [null, '']; - } - - let rangeToReplace = new vscode.Range(position.line, result.location, position.line, result.location + result.abbreviation.length); - return [rangeToReplace, result.abbreviation]; -} /** - * Inside