55
66import * as vscode from 'vscode' ;
77import { expand } from '@emmetio/expand-abbreviation' ;
8- import * as extract from '@emmetio/extract-abbreviation' ;
98import parseStylesheet from '@emmetio/css-parser' ;
109import parse from '@emmetio/html-matcher' ;
1110import Node from '@emmetio/node' ;
12-
13- import { getSyntax , getProfile , getVariables , isStyleSheet , getNode , getInnerRange } from './util' ;
14- import { DocumentStreamReader } from './bufferStream' ;
15-
16- const field = ( index , placeholder ) => `\${${ index } ${ placeholder ? ':' + placeholder : '' } }` ;
11+ import { getSyntax } from './util' ;
12+ import { getExpandOptions , extractAbbreviation , isStyleSheet , getNode , getInnerRange } from './emmetForVSCode/emmetUtils' ;
13+ import { DocumentStreamReader } from './emmetForVSCode/bufferStream' ;
1714
1815export function wrapWithAbbreviation ( ) {
1916 let editor = vscode . window . activeTextEditor ;
@@ -45,89 +42,37 @@ export function expandAbbreviation(args) {
4542 if ( typeof args !== 'object' || ! args [ 'syntax' ] ) {
4643 return ;
4744 }
48- let output = expandAbbreviationHelper ( args [ 'syntax' ] , editor . document , editor . selection ) ;
49- if ( output ) {
50- editor . insertSnippet ( new vscode . SnippetString ( output . expandedText ) , output . abbreviationRange ) ;
51- }
52- }
53-
54- export interface ExpandAbbreviationHelperOutput {
55- expandedText : string ;
56- abbreviationRange : vscode . Range ;
57- abbreviation : string ;
58- syntax : string ;
59- }
60-
61- /**
62- * Expands abbreviation at given range in the given document
63- * @param syntax string syntax to be used for expanding abbreviations
64- * @param document vscode.TextDocument
65- * @param abbreviationRange vscode.Range range of the abbreviation that needs to be expanded
66- * */
67- export function expandAbbreviationHelper ( syntax : string , document : vscode . TextDocument , abbreviationRange : vscode . Range ) : ExpandAbbreviationHelperOutput {
68- if ( ! syntax ) {
69- return ;
70- }
71- let abbreviation = document . getText ( abbreviationRange ) ;
45+ let syntax = args [ 'syntax' ] ;
46+ let abbreviationRange : vscode . Range = editor . selection ;
47+ let position = editor . selection . isReversed ? editor . selection . anchor : editor . selection . active ;
48+ let abbreviation = editor . document . getText ( abbreviationRange ) ;
7249 if ( abbreviationRange . isEmpty ) {
73- [ abbreviationRange , abbreviation ] = extractAbbreviation ( document , abbreviationRange . start ) ;
50+ [ abbreviationRange , abbreviation ] = extractAbbreviation ( editor . document , position ) ;
7451 }
7552
76- let expandedText = expand ( abbreviation , getExpandOptions ( syntax ) ) ;
77- return { expandedText, abbreviationRange, abbreviation, syntax } ;
78- }
79-
80- /**
81- * Checks whether given position is valid for emmet abbreviation and returns appropriate syntax
82- * @param syntax string language mode of current document
83- * @param document vscode.Textdocument
84- * @param position vscode.Position position of the abbreviation that needs to be expanded
85- */
86- export function syntaxHelper ( syntax : string , document : vscode . TextDocument , position : vscode . Position ) : string {
8753 let parseContent = isStyleSheet ( syntax ) ? parseStylesheet : parse ;
88- let rootNode : Node = parseContent ( new DocumentStreamReader ( document ) ) ;
54+ let rootNode : Node = parseContent ( new DocumentStreamReader ( editor . document ) ) ;
8955 let currentNode = getNode ( rootNode , position ) ;
9056
91- if ( forceCssSyntax ( syntax , currentNode , position ) ) {
92- return 'css' ;
93- } else if ( ! isValidLocationForEmmetAbbreviation ( currentNode , syntax , position ) ) {
57+ if ( ! isValidLocationForEmmetAbbreviation ( currentNode , syntax , position ) ) {
9458 return ;
9559 }
96- return syntax ;
97- }
9860
99- /**
100- * Extracts abbreviation from the given position in the given document
101- */
102- function extractAbbreviation ( document : vscode . TextDocument , position : vscode . Position ) : [ vscode . Range , string ] {
103- let currentLine = document . lineAt ( position . line ) . text ;
104- let result = extract ( currentLine , position . character , true ) ;
105- if ( ! result ) {
106- return [ null , '' ] ;
61+ let expandedText = expand ( abbreviation , getExpandOptions ( syntax ) ) ;
62+ if ( expandedText ) {
63+ editor . insertSnippet ( new vscode . SnippetString ( expandedText ) , abbreviationRange ) ;
10764 }
108-
109- let rangeToReplace = new vscode . Range ( position . line , result . location , position . line , result . location + result . abbreviation . length ) ;
110- return [ rangeToReplace , result . abbreviation ] ;
11165}
11266
113- /**
114- * Inside <style> tag, force use of css abbreviations
115- */
116- function forceCssSyntax ( syntax : string , currentNode : Node , position : vscode . Position ) : boolean {
117- return ! isStyleSheet ( syntax )
118- && currentNode
119- && currentNode . close
120- && currentNode . name === 'style'
121- && getInnerRange ( currentNode ) . contains ( position ) ;
122- }
12367
12468/**
125- * Checks if given position is a valid location to expand emmet abbreviation
69+ * Checks if given position is a valid location to expand emmet abbreviation.
70+ * Works only on html and css/less/scss syntax
12671 * @param currentNode parsed node at given position
12772 * @param syntax syntax of the abbreviation
12873 * @param position position to validate
12974 */
130- function isValidLocationForEmmetAbbreviation ( currentNode : Node , syntax : string , position : vscode . Position ) : boolean {
75+ export function isValidLocationForEmmetAbbreviation ( currentNode : Node , syntax : string , position : vscode . Position ) : boolean {
13176 if ( ! currentNode ) {
13277 return true ;
13378 }
@@ -142,15 +87,4 @@ function isValidLocationForEmmetAbbreviation(currentNode: Node, syntax: string,
14287 }
14388
14489 return false ;
145- }
146-
147- export function getExpandOptions ( syntax : string , textToReplace ?: string ) {
148- return {
149- field : field ,
150- syntax : syntax ,
151- profile : getProfile ( syntax ) ,
152- addons : syntax === 'jsx' ? { 'jsx' : true } : null ,
153- variables : getVariables ( ) ,
154- text : textToReplace ? textToReplace : ''
155- } ;
15690}
0 commit comments