Skip to content

Commit 8ccd34a

Browse files
committed
Optimize
1 parent 0e55ce7 commit 8ccd34a

File tree

2 files changed

+37
-32
lines changed

2 files changed

+37
-32
lines changed

src/analyzer.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ function parseFileAnalysis(filePath: string, sourceText: string): FileAnalysis {
192192
filePath,
193193
sourceText,
194194
ts.ScriptTarget.Latest,
195-
true,
195+
false,
196196
getScriptKind(filePath),
197197
)
198198

@@ -430,15 +430,11 @@ function getTagRanges(
430430
function getRootIdentifier(
431431
expression: ts.Expression,
432432
): ts.Identifier | undefined {
433-
if (ts.isIdentifier(expression)) {
434-
return expression
435-
}
436-
437-
if (ts.isPropertyAccessExpression(expression)) {
438-
return getRootIdentifier(expression.expression)
433+
let current = expression
434+
while (ts.isPropertyAccessExpression(current)) {
435+
current = current.expression
439436
}
440-
441-
return undefined
437+
return ts.isIdentifier(current) ? current : undefined
442438
}
443439

444440
function getScriptKind(filePath: string): ts.ScriptKind {

src/extension.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { type HighlightColors, LensDecorations } from './decorations'
88
import { ImportResolver, type SourceHost } from './resolver'
99

1010
const SUPPORTED_LANGUAGE_IDS = new Set(['javascriptreact', 'typescriptreact'])
11-
const WATCH_PATTERNS = ['**/*.{js,jsx,ts,tsx}', '**/{tsconfig,jsconfig}.json']
11+
const SOURCE_WATCH_PATTERN = '**/*.{js,jsx,ts,tsx}'
12+
const CONFIG_WATCH_PATTERN = '**/{tsconfig,jsconfig}.json'
1213
const DEFAULT_HIGHLIGHT_COLORS: HighlightColors = {
1314
clientComponent: '#14b8a6',
1415
serverComponent: '#f59e0b',
@@ -64,6 +65,29 @@ export function activate(context: vscode.ExtensionContext): void {
6465
decorations.apply(editor, usages)
6566
}
6667

68+
const createWatcher = (
69+
folder: vscode.WorkspaceFolder,
70+
pattern: string,
71+
onChange: (uri: vscode.Uri) => void,
72+
): void => {
73+
const watcher = vscode.workspace.createFileSystemWatcher(
74+
new vscode.RelativePattern(folder, pattern),
75+
)
76+
watcher.onDidChange(onChange, undefined, context.subscriptions)
77+
watcher.onDidCreate(
78+
() => clearCachesAndRefresh(),
79+
undefined,
80+
context.subscriptions,
81+
)
82+
watcher.onDidDelete(
83+
() => clearCachesAndRefresh(),
84+
undefined,
85+
context.subscriptions,
86+
)
87+
watcherDisposables.push(watcher)
88+
context.subscriptions.push(watcher)
89+
}
90+
6791
const registerWatchers = (): void => {
6892
for (const disposable of watcherDisposables) {
6993
disposable.dispose()
@@ -72,28 +96,13 @@ export function activate(context: vscode.ExtensionContext): void {
7296
watcherDisposables = []
7397

7498
for (const workspaceFolder of vscode.workspace.workspaceFolders ?? []) {
75-
for (const pattern of WATCH_PATTERNS) {
76-
const watcher = vscode.workspace.createFileSystemWatcher(
77-
new vscode.RelativePattern(workspaceFolder, pattern),
78-
)
79-
watcher.onDidChange(
80-
() => clearCachesAndRefresh(),
81-
undefined,
82-
context.subscriptions,
83-
)
84-
watcher.onDidCreate(
85-
() => clearCachesAndRefresh(),
86-
undefined,
87-
context.subscriptions,
88-
)
89-
watcher.onDidDelete(
90-
() => clearCachesAndRefresh(),
91-
undefined,
92-
context.subscriptions,
93-
)
94-
watcherDisposables.push(watcher)
95-
context.subscriptions.push(watcher)
96-
}
99+
createWatcher(workspaceFolder, SOURCE_WATCH_PATTERN, (uri) => {
100+
analyzer.invalidateFile(uri.fsPath)
101+
scheduleRefresh()
102+
})
103+
createWatcher(workspaceFolder, CONFIG_WATCH_PATTERN, () =>
104+
clearCachesAndRefresh(),
105+
)
97106
}
98107
}
99108

0 commit comments

Comments
 (0)