-
-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support SFC style CSS variable injection
vuejs/rfcs#231, close #335
- Loading branch information
1 parent
4af8f29
commit c5b375d
Showing
10 changed files
with
167 additions
and
29 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
34 changes: 34 additions & 0 deletions
34
packages/vscode-vue-languageservice/src/parsers/cssBinds.ts
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,34 @@ | ||
import type * as css from 'vscode-css-languageservice'; | ||
import type { TextRange } from './types'; | ||
|
||
type StylesheetNode = { | ||
children: StylesheetNode[] | undefined, | ||
end: number, | ||
length: number, | ||
offset: number, | ||
parent: Node | null, | ||
type: number, | ||
}; | ||
|
||
export function parse(docText: string, ss: css.Stylesheet) { | ||
const result: TextRange[] = []; | ||
visChild(ss as StylesheetNode); | ||
function visChild(node: StylesheetNode) { | ||
if (node.type === 22) { | ||
const nodeText = docText.substring(node.offset, node.end); | ||
const reg = /^v-bind\s*\(\s*(\S*)\s*\)$/; | ||
const match = nodeText.match(reg); | ||
if (match) { | ||
const matchText = match[1]; | ||
const offset = node.offset + nodeText.lastIndexOf(matchText); | ||
result.push({ start: offset, end: offset + matchText.length }); | ||
} | ||
} | ||
else if (node.children) { | ||
for (let i = 0; i < node.children.length; i++) { | ||
visChild(node.children[i]); | ||
} | ||
} | ||
} | ||
return result; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script lang="ts" setup> | ||
const foo = 1; | ||
</script> | ||
|
||
<style> | ||
.bar { color: v-bind(foo); } | ||
</style> |
28 changes: 28 additions & 0 deletions
28
packages/vscode-vue-languageservice/tests/rename/cssVars.ts
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,28 @@ | ||
import * as path from 'upath'; | ||
import { Position } from 'vscode-languageserver/node'; | ||
import { defineRename } from '../utils/defineRename'; | ||
|
||
defineRename({ | ||
fileName: path.resolve(__dirname, '../../testCases/cssVars.vue'), | ||
actions: [ | ||
{ | ||
position: Position.create(1, 6), | ||
newName: 'baz', | ||
length: 4, | ||
}, | ||
{ | ||
position: Position.create(5, 21), | ||
newName: 'baz', | ||
length: 4, | ||
}, | ||
], | ||
result: ` | ||
<script lang="ts" setup> | ||
const baz = 1; | ||
</script> | ||
<style> | ||
.bar { color: v-bind(baz); } | ||
</style> | ||
`.trim(), | ||
}); |
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