Skip to content

Commit 293b49e

Browse files
committed
modify jump to definition to work according to runtime behavior
1 parent 0665cc6 commit 293b49e

File tree

1 file changed

+92
-30
lines changed

1 file changed

+92
-30
lines changed

client/modules/IDE/components/jump-to-definition.js

Lines changed: 92 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ function buildProjectSymbolTable(files, scriptOrder) {
6363
return symbolTable;
6464
}
6565

66+
const jumpToLineChAfterLoad = (targetFileId, pos) => {
67+
let tries = 10;
68+
const tryJump = () => {
69+
const stateNow = store.getState();
70+
const filesNow = selectFiles(stateNow);
71+
const freshTarget = filesNow.find((f) => f.id === targetFileId); // get fresh copy
72+
const cm = freshTarget?.cmInstance;
73+
74+
if (cm) {
75+
cm.setCursor(pos);
76+
cm.focus();
77+
} else if (tries-- > 0) {
78+
setTimeout(tryJump, 30);
79+
}
80+
};
81+
tryJump();
82+
};
83+
6684
export function jumpToDefinition(pos) {
6785
const state = store.getState();
6886
const files = selectFiles(state);
@@ -113,6 +131,80 @@ export function jumpToDefinition(pos) {
113131

114132
let found = false;
115133

134+
if (!found) {
135+
const scriptOrder = getScriptLoadOrder(files);
136+
137+
if (scriptOrder.length) {
138+
const projectSymbolTable =
139+
buildProjectSymbolTable(files, scriptOrder) || {};
140+
const globalSymbol = projectSymbolTable[varName];
141+
142+
if (globalSymbol) {
143+
for (let i = scriptOrder.length - 1; i >= 0; i--) {
144+
const scriptName = scriptOrder[i];
145+
const file = files.find((f) => f.name.endsWith(scriptName));
146+
if (!file) continue;
147+
148+
let ast;
149+
try {
150+
ast = parser.parse(file.content, {
151+
sourceType: 'script',
152+
plugins: ['jsx', 'typescript']
153+
});
154+
} catch {
155+
continue;
156+
}
157+
158+
let foundInThisFile = false;
159+
traverse(ast, {
160+
FunctionDeclaration(path) {
161+
if (path.node.id?.name === varName) {
162+
const targetFileObj = file;
163+
164+
const fileContent = targetFileObj.content;
165+
const beforeText = fileContent.slice(0, path.node.start);
166+
const line = beforeText.split('\n').length - 1;
167+
const ch = beforeText.split('\n').pop().length;
168+
169+
store.dispatch(setSelectedFile(targetFileObj.id));
170+
pos = { line, ch };
171+
cm.setCursor(pos);
172+
173+
announceToScreenReader(
174+
`Jumped to definition of ${varName} in ${file.name}`
175+
);
176+
foundInThisFile = true;
177+
path.stop();
178+
}
179+
},
180+
VariableDeclarator(path) {
181+
if (path.node.id?.name === varName) {
182+
const targetFileObj = file;
183+
184+
const fileContent = targetFileObj.content;
185+
const beforeText = fileContent.slice(0, path.node.start);
186+
const line = beforeText.split('\n').length - 1;
187+
const ch = beforeText.split('\n').pop().length;
188+
189+
store.dispatch(setSelectedFile(targetFileObj.id));
190+
pos = { line, ch };
191+
cm.setCursor(pos);
192+
193+
announceToScreenReader(
194+
`Jumped to definition of ${varName} in ${file.name}`
195+
);
196+
foundInThisFile = true;
197+
path.stop();
198+
}
199+
}
200+
});
201+
202+
if (foundInThisFile) break;
203+
}
204+
}
205+
}
206+
}
207+
116208
traverse(ast, {
117209
VariableDeclarator(path) {
118210
if (found) return;
@@ -216,36 +308,6 @@ export function jumpToDefinition(pos) {
216308
}
217309
});
218310
}
219-
if (!found) {
220-
const scriptOrder = getScriptLoadOrder(files);
221-
222-
const projectSymbolTable =
223-
buildProjectSymbolTable(files, scriptOrder) || {};
224-
const globalSymbol = projectSymbolTable[varName];
225-
226-
if (globalSymbol) {
227-
const targetFileObj = files.find((f) => f.name === globalSymbol.file);
228-
if (!targetFileObj) {
229-
return;
230-
}
231-
232-
store.dispatch(setSelectedFile(targetFileObj.id));
233-
234-
if (!targetFileObj.cmInstance) {
235-
return;
236-
}
237-
238-
const targetFileCM = targetFileObj.cmInstance;
239-
const defPos = targetFileCM.posFromIndex(globalSymbol.pos);
240-
targetFileCM.setCursor(defPos);
241-
targetFileCM.focus();
242-
243-
announceToScreenReader(
244-
`Jumped to definition of ${varName} in ${globalSymbol.file}`
245-
);
246-
found = true;
247-
}
248-
}
249311

250312
if (!found) {
251313
announceToScreenReader(`No definition found for ${varName}`, true);

0 commit comments

Comments
 (0)