Skip to content

Commit

Permalink
fix(jsx): parser edge
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-He95 committed Mar 3, 2024
1 parent 24f038f commit b2cbc15
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
"@unocss/transformer-directives": "^0.53.6",
"@unocss/transformer-variant-group": "^0.53.6",
"@vscode-use/createwebview": "^0.0.11",
"@vscode-use/utils": "^0.0.82",
"@vscode-use/utils": "^0.0.86",
"@vue/compiler-sfc": "^3.3.8",
"bumpp": "^9.2.0",
"eslint": "^8.54.0",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 16 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode'
import type { TextEditorDecorationType } from 'vscode'
import { addEventListener, createBottomBar, createRange, getActiveText, getActiveTextEditor, getConfiguration, getCopyText, getLineText, getLocale, getSelection, message, nextTick, registerCommand, setConfiguration, setCopyText, updateText } from '@vscode-use/utils'
import { addEventListener, createBottomBar, createPosition, createRange, getActiveText, getActiveTextEditor, getConfiguration, getCopyText, getCurrentFileUrl, getLineText, getLocale, getSelection, message, nextTick, registerCommand, setConfiguration, setCopyText, updateText } from '@vscode-use/utils'
import { findUp } from 'find-up'
import { toUnocssClass, transformStyleToUnocss } from 'transform-to-unocss-core'
import { rules, transformAttrs, transformClassAttr } from './transform'
Expand All @@ -15,7 +15,7 @@ const cacheMap = new LRUCache(5000)
export let toRemFlag = false
export let decorationType: TextEditorDecorationType
export async function activate(context: vscode.ExtensionContext) {
const activeTextEditor = vscode.window.activeTextEditor
const activeTextEditor = getActiveTextEditor()
if (!activeTextEditor)
return

Expand Down Expand Up @@ -211,17 +211,19 @@ export async function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.languages.registerHoverProvider(LANS, {
provideHover(document, position) {
// 获取当前选中的文本范围
const editor = vscode.window.activeTextEditor
const editor = getActiveTextEditor()
if (!editor)
return
// 移除样式
vscode.window.activeTextEditor?.setDecorations(decorationType, [])
editor.setDecorations(decorationType, [])
const selection = editor.selection
const wordRange = new vscode.Range(selection.start, selection.end)
let selectedText = editor.document.getText(wordRange)
const realRangeMap: any = []
if (!selectedText) {
const range = document.getWordRangeAtPosition(position) as any
if (!range)
return
let word = document.getText(range)
if (!word)
return
Expand Down Expand Up @@ -277,13 +279,12 @@ export async function activate(context: vscode.ExtensionContext) {

context.subscriptions.push(vscode.window.onDidChangeTextEditorVisibleRanges(() => {
// 移除装饰器
if (vscode.window.activeTextEditor)
vscode.window.activeTextEditor.setDecorations(decorationType, [])
getActiveTextEditor()?.setDecorations(decorationType, [])
}))

context.subscriptions.push(addEventListener('text-change', () => vscode.window.activeTextEditor?.setDecorations(decorationType, [])))
context.subscriptions.push(addEventListener('text-change', () => getActiveTextEditor()?.setDecorations(decorationType, [])))

context.subscriptions.push(addEventListener('selection-change', () => vscode.window.activeTextEditor?.setDecorations(decorationType, [])))
context.subscriptions.push(addEventListener('selection-change', () => getActiveTextEditor()?.setDecorations(decorationType, [])))

function setStyle(selectedUnocssText: string, rangeMap: vscode.Range[]) {
// 增加decorationType样式
Expand All @@ -303,7 +304,9 @@ export async function activate(context: vscode.ExtensionContext) {

let hasUnoConfig: string | undefined
const currentFolder = (vscode.workspace.workspaceFolders as any)?.[0]
const activeTextEditorUri = vscode.window.activeTextEditor?.document?.uri?.path
const activeTextEditorUri = getCurrentFileUrl()
if (!activeTextEditorUri)
return
// const completions: vscode.CompletionItem[] = []
// let unoCompletionsMap: any
const switchMagic = getConfiguration('unot').get('switchMagic')
Expand Down Expand Up @@ -337,7 +340,7 @@ export async function activate(context: vscode.ExtensionContext) {
bottomBar.show()

if (currentFolder)
await updateUnoStatus(vscode.window.activeTextEditor?.document.uri.fsPath)
await updateUnoStatus(getCurrentFileUrl())
if (presets.length)
rules.unshift(...presets)

Expand All @@ -348,7 +351,7 @@ export async function activate(context: vscode.ExtensionContext) {
})

context.subscriptions.push(addEventListener('text-save', (document: vscode.TextDocument) => {
const activeTextEditor = vscode.window.activeTextEditor
const activeTextEditor = getActiveTextEditor()
if (!isOpen || !hasUnoConfig || !activeTextEditor)
return
// 对文档保存后的内容进行处理
Expand All @@ -364,7 +367,7 @@ export async function activate(context: vscode.ExtensionContext) {
if (changeList.length) {
updateText((edit) => {
changeList.forEach((change: any) => {
edit.replace(new vscode.Range(new vscode.Position(change.start.line - 1, change.start.column), new vscode.Position(change.end.line - 1, change.end.column - 1)), change.content)
edit.replace(new vscode.Range(createPosition(change.start.line - 1, change.start.column), createPosition(change.end.line - 1, change.end.column - 1)), change.content)
})
})
nextTick(() => {
Expand All @@ -376,7 +379,7 @@ export async function activate(context: vscode.ExtensionContext) {

context.subscriptions.push(addEventListener('activeText-change', () =>
setTimeout(async () => {
const url = vscode.window.activeTextEditor?.document.uri.fsPath
const url = getCurrentFileUrl()
if (!url)
return
await updateUnoStatus(url)
Expand Down
58 changes: 40 additions & 18 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,24 +223,46 @@ function jsxDfs(children: any, parent: any, position: vscode.Position) {
if (type === 'JSXElement' || type === 'Element' || (type === 'ReturnStatement' && (argument.type === 'JSXElement' || argument.type === 'JSXFragment')))
isInTemplate = true

if (child.children)
children = child.children
else if (type === 'ExportNamedDeclaration')
children = child.declaration.body
else if (type === 'ObjectExpression')
children = child.properties
else if (type === 'Property' && child.value.type === 'FunctionExpression')
children = child.value.body.body
else if (type === 'ExportDefaultDeclaration')
children = child.declaration.arguments
else if (type === 'VariableDeclaration')
children = declarations
else if (type === 'VariableDeclarator')
children = init
else if (type === 'ReturnStatement')
children = argument
else if (type === 'JSXElement')
children = child.children
if (child.children) { children = child.children }
else if (type === 'ExportNamedDeclaration') { children = child.declaration }
else if (type === 'ObjectExpression') { children = child.properties }
else if (type === 'Property' && child.value.type === 'FunctionExpression') { children = child.value.body.body }
else if (type === 'ExportDefaultDeclaration') {
if (child.declaration.type === 'FunctionDeclaration')
children = child.declaration.body.body
else
children = child.declaration.arguments
}
else if (type === 'JSXExpressionContainer') {
if (child.expression.type === 'CallExpression') { children = child.expression.arguments }
else if (child.expression.type === 'ConditionalExpression') {
children = [
child.expression.alternate,
child.expression.consequent,
].filter(Boolean)
}
else { children = child.expression }
}
else if (type === 'TemplateLiteral') {
children = child.expressions
}
else if (type === 'ConditionalExpression') {
children = [
child.alternate,
child.consequent,
].filter(Boolean)
}
else if (type === 'ArrowFunctionExpression') {
children = child.body
}
else if (type === 'VariableDeclaration') { children = declarations }
else if (type === 'VariableDeclarator') { children = init }
else if (type === 'ReturnStatement') { children = argument }
else if (type === 'JSXElement') { children = child.children }
else if (type === 'ExportNamedDeclaration') { children = child.declaration.body }
else if (type === 'CallExpression') {
children = child.arguments
}
if (children && !Array.isArray(children))
children = [children]

Expand Down

0 comments on commit b2cbc15

Please sign in to comment.