-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
21 changed files
with
192 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,7 @@ schema_version = 1 | |
authors = ["mantou132 <[email protected]>"] | ||
repository = "https://github.com/mantou132/gem" | ||
snippets = "./snippets/typescript.json" | ||
|
||
[grammars.typescript] | ||
repository = "https://github.com/tree-sitter/tree-sitter-typescript" | ||
commit = "f975a621f4e7f532fe322e13c4f79495e0a7b2e7" |
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
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
Empty file.
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 |
---|---|---|
@@ -1,3 +1,10 @@ | ||
export const CSS_REG = /(?<start>\/\*\s*css\s*\*\/\s*`|(?<!`)(?:css|less|scss)\s*`)(?<content>.*?)(`(?=;|\s))/gis; | ||
export const HTML_REG = /(?<start>\/\*\s*html\s*\*\/\s*`|(?<!`)(?:html|raw)\s*`)(?<content>[^`]*)(`)/gi; | ||
export const COLOR_REG = /(?<start>'|")?(?<content>#([0-9a-fA-F]{8}|[0-9a-fA-F]{6}|[0-9a-fA-F]{3,4}))($1|\s*;)/gi; | ||
|
||
// 直接通过正则匹配 css 片段,通过条件的结束 ` 号匹配 | ||
export const CSS_REG = /(?<start>\/\*\s*css\s*\*\/\s*`|(?<!`)(?:css|less|scss)\s*`)(?<content>.*?)(`(?=;|,?\s*\)))/gis; | ||
// 直接通过正则匹配 style 片段,通过条件的结束 ` 号匹配 | ||
// 语言服务和高亮都只支持 styled 写法 | ||
export const STYLE_REG = /(?<start>\/\*\s*style\s*\*\/\s*`|(?<!`)styled?\s*`)(?<content>.*?)(`(?=,|\s*}\s*\)))/gis; | ||
|
||
// 处理后进行正则匹配,所以不需要验证后面的 ` 号 | ||
export const HTML_REG = /(?<start>\/\*\s*html\s*\*\/\s*`|(?<!`)(?:html|raw)\s*`)(?<content>[^`]*)(`)/gi; |
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,66 @@ | ||
// 只对 CSS 语法和属性做了简单的检查,不做值检查 | ||
// TODO: 激活扩展、打开工作区时需要自动诊断所有文件 | ||
// TODO: 使用 LRU 缓存 | ||
|
||
// eslint-disable-next-line import/no-unresolved | ||
import { workspace, languages, window, Range, Diagnostic } from 'vscode'; | ||
import { getCSSLanguageService as getCSSLanguageService } from 'vscode-css-languageservice'; | ||
import type { ExtensionContext, TextDocument } from 'vscode'; | ||
|
||
import { CSS_REG, STYLE_REG } from './constants'; | ||
import { createVirtualDocument, removeSlot } from './util'; | ||
|
||
const diagnosticCollection = languages.createDiagnosticCollection('gem'); | ||
const cssLanguageService = getCSSLanguageService(); | ||
|
||
const updateDiagnostic = (document: TextDocument) => { | ||
const diagnostics: Diagnostic[] = []; | ||
const text = document.getText(); | ||
|
||
const matchFragments = (regexp: RegExp, appendBefore: string, appendAfter: string) => { | ||
regexp.exec('null'); | ||
|
||
let match; | ||
while ((match = regexp.exec(text))) { | ||
const matchContent = match.groups!.content; | ||
const offset = match.index + match.groups!.start.length; | ||
const virtualDocument = createVirtualDocument('css', `${appendBefore}${removeSlot(matchContent)}${appendAfter}`); | ||
const vCss = cssLanguageService.parseStylesheet(virtualDocument); | ||
const oDiagnostics = cssLanguageService.doValidation(virtualDocument, vCss) as Diagnostic[]; | ||
for (const { message, range } of oDiagnostics) { | ||
const { start, end } = range; | ||
const startOffset = virtualDocument.offsetAt(start) - appendBefore.length + offset; | ||
const endOffset = virtualDocument.offsetAt(end) - appendBefore.length + offset; | ||
const nRange = new Range(document.positionAt(startOffset), document.positionAt(endOffset)); | ||
diagnostics.push(new Diagnostic(nRange, message)); | ||
} | ||
} | ||
}; | ||
|
||
matchFragments(CSS_REG, '', ''); | ||
matchFragments(STYLE_REG, ':host { ', ' }'); | ||
|
||
diagnosticCollection.set(document.uri, diagnostics); | ||
}; | ||
|
||
export function markDiagnostic(context: ExtensionContext) { | ||
context.subscriptions.push(diagnosticCollection); | ||
|
||
context.subscriptions.push( | ||
window.onDidChangeActiveTextEditor((editor) => { | ||
if (editor) { | ||
updateDiagnostic(editor.document); | ||
} | ||
}), | ||
); | ||
|
||
context.subscriptions.push( | ||
workspace.onDidChangeTextDocument(({ document }) => { | ||
updateDiagnostic(document); | ||
}), | ||
); | ||
|
||
if (window.activeTextEditor) { | ||
updateDiagnostic(window.activeTextEditor.document); | ||
} | ||
} |
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,30 @@ | ||
// eslint-disable-next-line import/no-unresolved | ||
import { ColorPresentation, ColorInformation, Range, Color } from 'vscode'; | ||
import { rgbToHexColor, parseHexColor } from 'duoyun-ui/lib/color'; | ||
import type { HexColor } from 'duoyun-ui/lib/color'; | ||
import type { DocumentColorProvider, TextDocument } from 'vscode'; | ||
|
||
import { COLOR_REG } from '../constants'; | ||
|
||
export class ColorProvider implements DocumentColorProvider { | ||
provideDocumentColors(document: TextDocument) { | ||
COLOR_REG.exec('null'); | ||
|
||
const documentText = document.getText(); | ||
const colors: ColorInformation[] = []; | ||
|
||
let match: RegExpExecArray | null; | ||
while ((match = COLOR_REG.exec(documentText)) !== null) { | ||
const hex = match.groups!.content as HexColor; | ||
const [red, green, blue, alpha] = parseHexColor(hex); | ||
const offset = match.index + (match.groups!.start?.length || 0); | ||
const range = new Range(document.positionAt(offset), document.positionAt(offset + hex.length)); | ||
colors.push(new ColorInformation(range, new Color(red / 255, green / 255, blue / 255, alpha))); | ||
} | ||
return colors; | ||
} | ||
|
||
provideColorPresentations({ red, green, blue, alpha }: Color) { | ||
return [new ColorPresentation(rgbToHexColor([red * 255, green * 255, blue * 255, alpha]))]; | ||
} | ||
} |
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
Oops, something went wrong.