mirror of
https://github.com/compiler-explorer/compiler-explorer.git
synced 2026-12-02 10:37:31 -06:00
Check lineNumber is valid before getting its tokens
This commit is contained in:
+25
-23
@@ -1038,32 +1038,34 @@ Editor.prototype.onSelectLine = function (id, lineNum) {
|
||||
// ^a ^column ^b
|
||||
Editor.prototype.getTokenSpan = function (lineNum, column) {
|
||||
var model = this.editor.getModel();
|
||||
var line = model.getLineContent(lineNum);
|
||||
if (0 < column && column < line.length) {
|
||||
var tokens = monaco.editor.tokenize(line, model.getModeId());
|
||||
if (tokens.length > 0) {
|
||||
var lastOffset = 0;
|
||||
var lastWasString = false;
|
||||
for (var i = 0; i < tokens[0].length; ++i) {
|
||||
// Treat all the contiguous string tokens as one,
|
||||
// For example "hello \" world" is treated as one token
|
||||
// instead of 3 "string.cpp", "string.escape.cpp", "string.cpp"
|
||||
if (tokens[0][i].type.startsWith('string')) {
|
||||
if (lastWasString) {
|
||||
continue;
|
||||
if (lineNum <= model.getLineCount()) {
|
||||
var line = model.getLineContent(lineNum);
|
||||
if (0 < column && column < line.length) {
|
||||
var tokens = monaco.editor.tokenize(line, model.getModeId());
|
||||
if (tokens.length > 0) {
|
||||
var lastOffset = 0;
|
||||
var lastWasString = false;
|
||||
for (var i = 0; i < tokens[0].length; ++i) {
|
||||
// Treat all the contiguous string tokens as one,
|
||||
// For example "hello \" world" is treated as one token
|
||||
// instead of 3 "string.cpp", "string.escape.cpp", "string.cpp"
|
||||
if (tokens[0][i].type.startsWith('string')) {
|
||||
if (lastWasString) {
|
||||
continue;
|
||||
}
|
||||
lastWasString = true;
|
||||
} else {
|
||||
lastWasString = false;
|
||||
}
|
||||
var currentOffset = tokens[0][i].offset;
|
||||
if (column <= currentOffset) {
|
||||
return {colBegin: lastOffset, colEnd: currentOffset};
|
||||
} else {
|
||||
lastOffset = currentOffset;
|
||||
}
|
||||
lastWasString = true;
|
||||
} else {
|
||||
lastWasString = false;
|
||||
}
|
||||
var currentOffset = tokens[0][i].offset;
|
||||
if (column <= currentOffset) {
|
||||
return {colBegin: lastOffset, colEnd: currentOffset};
|
||||
} else {
|
||||
lastOffset = currentOffset;
|
||||
}
|
||||
return {colBegin: lastOffset, colEnd: line.length};
|
||||
}
|
||||
return {colBegin: lastOffset, colEnd: line.length};
|
||||
}
|
||||
}
|
||||
return {colBegin: column, colEnd: column + 1};
|
||||
|
||||
Reference in New Issue
Block a user