Skip to content

Commit ea1cb8f

Browse files
committed
Add columns to Language Server
1 parent 5ad8b27 commit ea1cb8f

10 files changed

Lines changed: 68 additions & 50 deletions

File tree

core/object/script_language.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ class ScriptLanguage : public Object {
222222

223223
/* EDITOR FUNCTIONS */
224224
struct Warning {
225-
int start_line = -1, end_line = -1;
225+
int start_line = 0;
226+
int end_line = 0;
226227
int code;
227228
String string_code;
228229
String message;

modules/gdscript/gdscript.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -820,10 +820,10 @@ Error GDScript::reload(bool p_keep_state) {
820820
}
821821
if (err) {
822822
if (EngineDebugger::is_active()) {
823-
GDScriptLanguage::get_singleton()->debug_break_parse(_get_debug_path(), parser.get_errors().front()->get().line, "Parser Error: " + parser.get_errors().front()->get().message);
823+
GDScriptLanguage::get_singleton()->debug_break_parse(_get_debug_path(), parser.get_errors().front()->get().start_line, "Parser Error: " + parser.get_errors().front()->get().message);
824824
}
825825
// TODO: Show all error messages.
826-
_err_print_error("GDScript::reload", path.is_empty() ? "built-in" : (const char *)path.utf8().get_data(), parser.get_errors().front()->get().line, ("Parse Error: " + parser.get_errors().front()->get().message).utf8().get_data(), false, ERR_HANDLER_SCRIPT);
826+
_err_print_error("GDScript::reload", path.is_empty() ? "built-in" : (const char *)path.utf8().get_data(), parser.get_errors().front()->get().start_line, ("Parse Error: " + parser.get_errors().front()->get().message).utf8().get_data(), false, ERR_HANDLER_SCRIPT);
827827
reloading = false;
828828
return ERR_PARSE_ERROR;
829829
}
@@ -833,12 +833,12 @@ Error GDScript::reload(bool p_keep_state) {
833833

834834
if (err) {
835835
if (EngineDebugger::is_active()) {
836-
GDScriptLanguage::get_singleton()->debug_break_parse(_get_debug_path(), parser.get_errors().front()->get().line, "Parser Error: " + parser.get_errors().front()->get().message);
836+
GDScriptLanguage::get_singleton()->debug_break_parse(_get_debug_path(), parser.get_errors().front()->get().start_line, "Parser Error: " + parser.get_errors().front()->get().message);
837837
}
838838

839839
const List<GDScriptParser::ParserError>::Element *e = parser.get_errors().front();
840840
while (e != nullptr) {
841-
_err_print_error("GDScript::reload", path.is_empty() ? "built-in" : (const char *)path.utf8().get_data(), e->get().line, ("Parse Error: " + e->get().message).utf8().get_data(), false, ERR_HANDLER_SCRIPT);
841+
_err_print_error("GDScript::reload", path.is_empty() ? "built-in" : (const char *)path.utf8().get_data(), e->get().start_line, ("Parse Error: " + e->get().message).utf8().get_data(), false, ERR_HANDLER_SCRIPT);
842842
e = e->next();
843843
}
844844
reloading = false;

modules/gdscript/gdscript_editor.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ bool GDScriptLanguage::validate(const String &p_script, const String &p_path, Li
169169
for (const GDScriptParser::ParserError &pe : parser.get_errors()) {
170170
ScriptLanguage::ScriptError e;
171171
e.path = p_path;
172-
e.line = pe.line;
173-
e.column = pe.column;
172+
e.line = pe.start_line;
173+
e.column = pe.start_column;
174174
e.message = pe.message;
175175
r_errors->push_back(e);
176176
}
@@ -180,8 +180,8 @@ bool GDScriptLanguage::validate(const String &p_script, const String &p_path, Li
180180
for (const GDScriptParser::ParserError &pe : depended_parser->get_errors()) {
181181
ScriptLanguage::ScriptError e;
182182
e.path = E.key;
183-
e.line = pe.line;
184-
e.column = pe.column;
183+
e.line = pe.start_line;
184+
e.column = pe.start_column;
185185
e.message = pe.message;
186186
r_errors->push_back(e);
187187
}

modules/gdscript/gdscript_parser.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,22 @@ void GDScriptParser::push_error(const String &p_message, const Node *p_origin) {
235235
// TODO: Improve error reporting by pointing at source code.
236236
// TODO: Errors might point at more than one place at once (e.g. show previous declaration).
237237
panic_mode = true;
238-
// TODO: Improve positional information.
238+
ParserError err;
239+
err.message = p_message;
240+
239241
if (p_origin == nullptr) {
240-
errors.push_back({ p_message, previous.start_line, previous.start_column });
242+
err.start_line = previous.start_line;
243+
err.start_column = previous.start_column;
244+
err.end_line = previous.end_line;
245+
err.end_column = previous.end_column;
241246
} else {
242-
errors.push_back({ p_message, p_origin->start_line, p_origin->start_column });
247+
err.start_line = p_origin->start_line;
248+
err.start_column = p_origin->start_column;
249+
err.end_line = p_origin->end_line;
250+
err.end_column = p_origin->end_column;
243251
}
252+
253+
errors.push_back(err);
244254
}
245255

246256
#ifdef DEBUG_ENABLED
@@ -279,7 +289,9 @@ void GDScriptParser::apply_pending_warnings() {
279289
warning.code = pw.code;
280290
warning.symbols = pw.symbols;
281291
warning.start_line = pw.source->start_line;
292+
warning.start_column = pw.source->start_column;
282293
warning.end_line = pw.source->end_line;
294+
warning.end_column = pw.source->end_column;
283295

284296
if (pw.treated_as_error) {
285297
push_error(warning.get_message() + String(" (Warning treated as error.)"), pw.source);

modules/gdscript/gdscript_parser.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,10 @@ class GDScriptParser {
269269
// };
270270
// Type type = NO_ERROR;
271271
String message;
272-
int line = 0, column = 0;
272+
int start_line = 0;
273+
int start_column = 0;
274+
int end_line = 0;
275+
int end_column = 0;
273276
};
274277

275278
#ifdef TOOLS_ENABLED
@@ -337,8 +340,10 @@ class GDScriptParser {
337340
};
338341

339342
Type type = NONE;
340-
int start_line = 0, end_line = 0;
341-
int start_column = 0, end_column = 0;
343+
int start_line = 0;
344+
int start_column = 0;
345+
int end_line = 0;
346+
int end_column = 0;
342347
Node *next = nullptr;
343348
List<AnnotationNode *> annotations;
344349

@@ -1108,8 +1113,10 @@ class GDScriptParser {
11081113
StringName name;
11091114
FunctionNode *source_function = nullptr;
11101115

1111-
int start_line = 0, end_line = 0;
1112-
int start_column = 0, end_column = 0;
1116+
int start_line = 0;
1117+
int start_column = 0;
1118+
int end_line = 0;
1119+
int end_column = 0;
11131120

11141121
DataType get_datatype() const;
11151122
String get_name() const;

modules/gdscript/gdscript_tokenizer.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ class GDScriptTokenizer {
165165

166166
Type type = EMPTY;
167167
Variant literal;
168-
int start_line = 0, end_line = 0, start_column = 0, end_column = 0;
168+
int start_line = 0;
169+
int start_column = 0;
170+
int end_line = 0;
171+
int end_column = 0;
169172
CursorPlace cursor_place = CURSOR_NONE;
170173
String source;
171174

@@ -224,13 +227,16 @@ class GDScriptTokenizerText : public GDScriptTokenizer {
224227
String source;
225228
const char32_t *_source = nullptr;
226229
const char32_t *_current = nullptr;
227-
int line = -1, column = -1;
228-
int cursor_line = -1, cursor_column = -1;
230+
int line = 0;
231+
int column = 0;
232+
int cursor_line = -1;
233+
int cursor_column = -1;
229234
int tab_size = 4;
230235

231236
// Keep track of multichar tokens.
232237
const char32_t *_start = nullptr;
233-
int start_line = 0, start_column = 0;
238+
int start_line = 0;
239+
int start_column = 0;
234240

235241
// Info cache.
236242
bool line_continuation = false; // Whether this line is a continuation of the previous, like when using '\'.

modules/gdscript/gdscript_warning.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ class GDScriptWarning {
158158
static_assert(std_size(default_warning_levels) == WARNING_MAX, "Amount of default levels does not match the amount of warnings.");
159159

160160
Code code = WARNING_MAX;
161-
int start_line = -1, end_line = -1;
161+
int start_line = 0;
162+
int start_column = 0;
163+
int end_line = 0;
164+
int end_column = 0;
162165
Vector<String> symbols;
163166

164167
String get_name() const;

modules/gdscript/language_server/gdscript_extend_parser.cpp

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,12 @@ void ExtendGDScriptParser::update_diagnostics() {
135135
diagnostic.severity = LSP::DiagnosticSeverity::Error;
136136
diagnostic.message = error.message;
137137
diagnostic.source = "gdscript";
138-
diagnostic.code = -1;
139-
LSP::Range range;
140-
LSP::Position pos;
141-
const PackedStringArray line_array = get_lines();
142-
int line = CLAMP(LINE_NUMBER_TO_INDEX(error.line), 0, line_array.size() - 1);
143-
const String &line_text = line_array[line];
144-
pos.line = line;
145-
pos.character = line_text.length() - line_text.strip_edges(true, false).length();
146-
range.start = pos;
147-
range.end = range.start;
148-
range.end.character = line_text.strip_edges(false).length();
149-
diagnostic.range = range;
138+
139+
GodotRange godot_range(
140+
GodotPosition(error.start_line, error.start_column),
141+
GodotPosition(error.end_line, error.end_column));
142+
143+
diagnostic.range = godot_range.to_lsp(get_lines());
150144
diagnostics.push_back(diagnostic);
151145
}
152146

@@ -156,17 +150,12 @@ void ExtendGDScriptParser::update_diagnostics() {
156150
diagnostic.severity = LSP::DiagnosticSeverity::Warning;
157151
diagnostic.message = "(" + warning.get_name() + "): " + warning.get_message();
158152
diagnostic.source = "gdscript";
159-
diagnostic.code = warning.code;
160-
LSP::Range range;
161-
LSP::Position pos;
162-
int line = LINE_NUMBER_TO_INDEX(warning.start_line);
163-
const String &line_text = get_lines()[line];
164-
pos.line = line;
165-
pos.character = line_text.length() - line_text.strip_edges(true, false).length();
166-
range.start = pos;
167-
range.end = pos;
168-
range.end.character = line_text.strip_edges(false).length();
169-
diagnostic.range = range;
153+
154+
GodotRange godot_range(
155+
GodotPosition(warning.start_line, warning.start_column),
156+
GodotPosition(warning.end_line, warning.end_column));
157+
158+
diagnostic.range = godot_range.to_lsp(get_lines());
170159
diagnostics.push_back(diagnostic);
171160
}
172161
}

modules/gdscript/tests/gdscript_test_runner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ GDScriptTest::TestResult GDScriptTest::execute_test_code(bool p_is_generating) {
575575

576576
StringBuilder error_string;
577577
for (const GDScriptParser::ParserError &error : parser.get_errors()) {
578-
error_string.append(vformat(">> ERROR at line %d: %s\n", error.line, error.message));
578+
error_string.append(vformat(">> ERROR at line %d: %s\n", error.start_line, error.message));
579579
}
580580
result.output += error_string.as_string();
581581
if (!p_is_generating) {

modules/gdscript/tests/test_gdscript.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ static void test_parser(const String &p_code, const String &p_script_path, const
175175
if (err != OK) {
176176
const List<GDScriptParser::ParserError> &errors = parser.get_errors();
177177
for (const GDScriptParser::ParserError &error : errors) {
178-
print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
178+
print_line(vformat("%02d:%02d: %s", error.start_line, error.start_column, error.message));
179179
}
180180
}
181181

@@ -185,7 +185,7 @@ static void test_parser(const String &p_code, const String &p_script_path, const
185185
if (err != OK) {
186186
const List<GDScriptParser::ParserError> &errors = parser.get_errors();
187187
for (const GDScriptParser::ParserError &error : errors) {
188-
print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
188+
print_line(vformat("%02d:%02d: %s", error.start_line, error.start_column, error.message));
189189
}
190190
}
191191

@@ -261,7 +261,7 @@ static void test_compiler(const String &p_code, const String &p_script_path, con
261261
print_line("Error in parser:");
262262
const List<GDScriptParser::ParserError> &errors = parser.get_errors();
263263
for (const GDScriptParser::ParserError &error : errors) {
264-
print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
264+
print_line(vformat("%02d:%02d: %s", error.start_line, error.start_column, error.message));
265265
}
266266
return;
267267
}
@@ -273,7 +273,7 @@ static void test_compiler(const String &p_code, const String &p_script_path, con
273273
print_line("Error in analyzer:");
274274
const List<GDScriptParser::ParserError> &errors = parser.get_errors();
275275
for (const GDScriptParser::ParserError &error : errors) {
276-
print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
276+
print_line(vformat("%02d:%02d: %s", error.start_line, error.start_column, error.message));
277277
}
278278
return;
279279
}

0 commit comments

Comments
 (0)