From 209c8f4de6df2cdf64052ec618b7fc4d6438fe0e Mon Sep 17 00:00:00 2001 From: RabsRincon Date: Sat, 24 Jul 2021 00:12:24 +0200 Subject: [PATCH] Check lineNumber is valid before getting its tokens --- static/panes/editor.js | 48 ++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/static/panes/editor.js b/static/panes/editor.js index d62d873dc..31cd77a33 100644 --- a/static/panes/editor.js +++ b/static/panes/editor.js @@ -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};