-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d4e254
commit b921b16
Showing
5 changed files
with
90 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { TextDocument, Position } from 'vscode-languageserver'; | ||
|
||
export function isVueFile(path: string) { | ||
return path.endsWith('.vue'); | ||
} | ||
|
||
export function getVueSCSSRegions(content: string) { | ||
const regions: Array<[number, number]> = []; | ||
const startRe = /<style[\w=\"\' \n\t]{1,}lang=[\"\']scss[\"\'][\w=\"\' \n\t]{0,}>/g; | ||
const endRe = /<\/style>/g; | ||
/* tslint:disable:no-conditional-assignment */ | ||
let start: RegExpExecArray; | ||
let end: RegExpExecArray; | ||
while ((start = startRe.exec(content)) !== null && (end = endRe.exec(content)) !== null) { | ||
regions.push([start.index + start[0].length, end.index]); | ||
} | ||
return regions; | ||
} | ||
|
||
export function getVueSCSSContent(content: string) { | ||
const oldContent = content; | ||
|
||
let newContent = oldContent | ||
.split('\n') | ||
.map(line => ' '.repeat(line.length)) | ||
.join('\n'); | ||
|
||
for (const r of getVueSCSSRegions(oldContent)) { | ||
newContent = newContent.slice(0, r[0]) + oldContent.slice(r[0], r[1]) + newContent.slice(r[1]); | ||
} | ||
|
||
return newContent; | ||
} | ||
|
||
function convertVueTextDocument(document: TextDocument) { | ||
return TextDocument.create(document.uri, 'scss', document.version, getVueSCSSContent(document.getText())); | ||
} | ||
|
||
export function getSCSSRegionsDocument(document: TextDocument, position: Position) { | ||
const offset = document.offsetAt(position); | ||
if (isVueFile(document.uri)) { | ||
const vueSCSSRegions = getVueSCSSRegions(document.getText()); | ||
if (vueSCSSRegions.some(region => region[0] <= offset && region[1] >= offset)) { | ||
return { document: convertVueTextDocument(document), offset }; | ||
} | ||
return { document: null, offset }; | ||
} | ||
|
||
return { document, offset }; | ||
} |