Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit 8322e0b

Browse files
author
mattpass
committed
Revised jumpToDefinition
1 parent 14e2256 commit 8322e0b

File tree

1 file changed

+76
-66
lines changed

1 file changed

+76
-66
lines changed

assets/js/icecoder.js

Lines changed: 76 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ var ICEcoder = {
4949
colorDropTgtBGFile: '#f80', // Drop dir target background color
5050
prevTab: 0, // Previous tab to current
5151
serverQueueItems: [], // Array of URLs to call in order
52+
origCursorPos: false, // Original cursor position before jump to definition
53+
origSelectionPos: false, // Original selection position before jump to definition
5254
previewWindow: false, // Target variable for the preview window
5355
previewWindowLoading: false, // Loading state of preview window
5456
pluginIntervalRefs: [], // Array of plugin interval refs
@@ -685,50 +687,7 @@ var ICEcoder = {
685687
// If CTRL key down
686688
if (evt.ctrlKey) {
687689
setTimeout(function(ic) {
688-
// Get cM and word under mouse pointer
689-
let cM = thisCM;
690-
let word = (cM.getRange(cM.findWordAt(cM.getCursor()).anchor, cM.findWordAt(cM.getCursor()).head));
691-
692-
// Get result and number of results for word in functions and classes from index JSON object list
693-
let result = null;
694-
let numResults = 0;
695-
let filePath = ic.openFiles[ic.selectedTab - 1];
696-
let filePathExt = filePath.substr(filePath.lastIndexOf(".") + 1);
697-
698-
if ("undefined" !== typeof ic.indexData.functions) {
699-
for(i in ic.indexData.functions[filePathExt]) {
700-
if (i === word) {
701-
result = ic.indexData.functions[filePathExt][i];
702-
numResults++;
703-
}
704-
}
705-
}
706-
707-
if ("undefined" !== typeof ic.indexData.class) {
708-
for (i in ic.indexData.classes[filePathExt]) {
709-
if (i === word) {
710-
result = ic.indexData.classes[filePathExt][i];
711-
numResults++;
712-
}
713-
}
714-
}
715-
716-
// If we have a single result and the cursor isn't already on the definition of it we can jump to where it's defined
717-
if (1 === numResults && -1 === [null, "def"].indexOf(cM.getTokenTypeAt(cM.getCursor()))) {
718-
ic.openFile(result.filePath.replace(docRoot, ""));
719-
ic.goFindAfterOpenInt = setInterval(function(result) {
720-
if (ic.openFiles[ic.selectedTab - 1] == result.filePath.replace(docRoot, "") && !ic.loadingFile) {
721-
cM = ic.getcMInstance();
722-
setTimeout(function(result) {
723-
ic.goToLine(result.range.from.line + 1);
724-
cM.setSelection({line: result.range.from.line, ch: result.range.from.ch}, {line: result.range.to.line, ch: result.range.to.ch});
725-
}, 20, result);
726-
clearInterval(ic.goFindAfterOpenInt);
727-
}
728-
}, 20, result);
729-
}
730-
731-
ic.mouseDownInCM = "editor";
690+
ic.jumpToDefinition();
732691
}, 0, this);
733692
}
734693
},
@@ -1352,34 +1311,85 @@ var ICEcoder = {
13521311
}
13531312
},
13541313

1355-
// Jump to and highlight the function definition current token
1314+
// Jump to and highlight the function definition current token or back again to where we were
13561315
jumpToDefinition: function() {
1357-
let thisCM, tokenString, defVars;
1316+
let thisCM, word, cursorBack1Char, result, numResults, filePath, filePathExt;
13581317

13591318
thisCM = this.getThisCM();
13601319

1361-
tokenString = thisCM.getTokenAt(thisCM.getCursor()).string;
1362-
1363-
if (thisCM.somethingSelected() && this.origCurorPos) {
1364-
thisCM.setCursor(this.origCurorPos);
1320+
// We have an original cursor or selection position, so we'll jump back to it
1321+
if (false !== this.origCursorPos || false !== this.origSelectionPos) {
1322+
// Jump to the original position (selection/cursor) we have and set selection/cursor
1323+
if (false !== this.origSelectionPos) {
1324+
this.goToLine(this.origSelectionPos.anchor.line + 1);
1325+
thisCM.setSelection(this.origSelectionPos.anchor, this.origSelectionPos.head);
1326+
} else {
1327+
this.goToLine(this.origCursorPos.line + 1);
1328+
thisCM.setCursor(this.origCursorPos);
1329+
}
1330+
// Reset flags for next time
1331+
this.origSelectionPos = false;
1332+
this.origCursorPos = false;
13651333
} else {
1366-
this.origCurorPos = thisCM.getCursor();
1367-
defVars = [
1368-
"var " + tokenString,
1369-
"function " + tokenString,
1370-
tokenString + "=function", tokenString + "= function", tokenString + " =function", tokenString + " = function",
1371-
tokenString + "=new function", tokenString + "= new function", tokenString + " =new function", tokenString + " = new function",
1372-
"window['" + tokenString + "']", "window[\"" + tokenString + "\"]",
1373-
"this['" + tokenString + "']", "this[\"" + tokenString + "\"]",
1374-
tokenString + ":", tokenString + " :",
1375-
"def " + tokenString,
1376-
"class " + tokenString
1377-
];
1378-
for (let i = 0; i < defVars.length; i++) {
1379-
if (this.findReplace(defVars[i], false, false, false)) {
1380-
break;
1334+
// Set flags for original section or cursor so we can return next time
1335+
if (thisCM.listSelections()[0]) {
1336+
this.origSelectionPos = thisCM.listSelections()[0];
1337+
} else {
1338+
this.origCursorPos = thisCM.getCursor();
1339+
}
1340+
1341+
// Get word
1342+
word = thisCM.getRange(thisCM.findWordAt(thisCM.getCursor()).anchor, thisCM.findWordAt(thisCM.getCursor()).head);
1343+
// If we got parens, try back 1 character to get word
1344+
if (-1 < word.indexOf("(")) {
1345+
cursorBack1Char = {line: thisCM.getCursor().line, ch: thisCM.getCursor().ch -1};
1346+
word = thisCM.getRange(thisCM.findWordAt(cursorBack1Char).anchor, thisCM.findWordAt(cursorBack1Char).head);
1347+
}
1348+
1349+
// Set start point for result and number of results for word in functions and classes from index JSON object list
1350+
result = null;
1351+
numResults = 0;
1352+
// Identify the file path and extension
1353+
filePath = this.openFiles[this.selectedTab - 1];
1354+
filePathExt = filePath.substr(filePath.lastIndexOf(".") + 1);
1355+
1356+
// Find the word within extention type in functions list data
1357+
if ("undefined" !== typeof this.indexData.functions) {
1358+
for(i in this.indexData.functions[filePathExt]) {
1359+
if (i === word) {
1360+
result = this.indexData.functions[filePathExt][i];
1361+
numResults++;
1362+
}
13811363
}
13821364
}
1365+
1366+
// Find the word within extention type in classes list data
1367+
if ("undefined" !== typeof this.indexData.class) {
1368+
for (i in this.indexData.classes[filePathExt]) {
1369+
if (i === word) {
1370+
result = this.indexData.classes[filePathExt][i];
1371+
numResults++;
1372+
}
1373+
}
1374+
}
1375+
1376+
// If we have a single result and the cursor isn't already on the definition of it, we can jump to where it's defined
1377+
if (1 === numResults && -1 === [null, "def"].indexOf(thisCM.getTokenTypeAt(thisCM.getCursor()))) {
1378+
// Open file (or switch tab to it if already open) and find when editor showing
1379+
this.openFile(result.filePath.replace(docRoot, ""));
1380+
this.goFindAfterOpenInt = setInterval(function(result) {
1381+
if (ICEcoder.openFiles[ICEcoder.selectedTab - 1] == result.filePath.replace(docRoot, "") && !ICEcoder.loadingFile) {
1382+
thisCM = ICEcoder.getcMInstance();
1383+
setTimeout(function(result) {
1384+
ICEcoder.goToLine(result.range.from.line + 1);
1385+
thisCM.setSelection({line: result.range.from.line, ch: result.range.from.ch}, {line: result.range.to.line, ch: result.range.to.ch});
1386+
}, 20, result);
1387+
clearInterval(ICEcoder.goFindAfterOpenInt);
1388+
}
1389+
}, 20, result);
1390+
}
1391+
1392+
this.mouseDownInCM = "editor";
13831393
}
13841394
},
13851395

@@ -4826,7 +4836,7 @@ var ICEcoder = {
48264836
return false;
48274837

48284838
// CTRL/Cmd + numeric minus (Close tab)
4829-
} else if ((key==109 || key==189) && true === ctrlOrCmd) {
4839+
} else if ((109 === key || 189 === key) && true === ctrlOrCmd) {
48304840
"content" === area
48314841
? this.removeLines()
48324842
: this.closeTab(this.selectedTab);

0 commit comments

Comments
 (0)