Skip to content

Commit d698768

Browse files
committed
Show filename, line, column in error messages coming from parsing errors
1 parent fd01632 commit d698768

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

server/utextdocument.pas

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,7 @@ procedure GetCompletionRecords(
229229
if not CodeToolBoss.GatherIdentifiers(Code, X, Y) then
230230
raise ERpcError.Create(
231231
jsrpcRequestFailed,
232-
Format('Line %d: %s', [
233-
CodeToolBoss.ErrorLine,
234-
CodeToolBoss.ErrorMessage
235-
])
236-
);
232+
PositionForErrorPrefix(CodeToolBoss) + CodeToolBoss.ErrorMessage);
237233

238234
Count := CodeToolBoss.IdentifierList.GetFilteredCount;
239235

@@ -992,7 +988,7 @@ procedure TextDocument_JumpTo(
992988
// has id 20170421200105 so we then do not show message window in vscode
993989
if E.Id <> 20170421200105 then
994990
begin
995-
ShowErrorMessage(Rpc, E.Message);
991+
ShowErrorMessage(Rpc, PositionForErrorPrefix(E) + E.Message);
996992
end;
997993
end;
998994
{ ELinkScannerError is raised from FindDeclaration e.g. when include file is missing.

server/uutils.pas

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,25 @@
2323

2424
interface
2525

26+
uses CustomCodeTool, CodeToolManager, CodeCache;
27+
2628
function MergePaths(Paths: array of string): string;
2729
function GetConfigDirForApp(AppName, Vendor: string; Global: Boolean): string;
2830
function URIToFileNameEasy(const UriStr: String): String;
2931

32+
{ Return prefix for error message describing filename, line, column
33+
from ECodeToolError, if any. }
34+
function PositionForErrorPrefix(const E: ECodeToolError): String; overload;
35+
36+
{ Return prefix for error message describing filename, line, column
37+
from TCodeToolManager, if any. }
38+
function PositionForErrorPrefix(const CodeToolBoss: TCodeToolManager): String; overload;
39+
3040
implementation
3141

3242
uses
33-
SysUtils, URIParser, ujsonrpc;
43+
SysUtils, URIParser,
44+
ujsonrpc;
3445

3546
function MergePaths(Paths: array of string): string;
3647
var
@@ -94,6 +105,63 @@ function URIToFileNameEasy(const UriStr: String): String;
94105
);
95106
end;
96107

108+
const
109+
{ Error prefix to display filename (may be ''), line, column.
110+
Note: line endings (#10, #13 or both) are ignored inside this, at least by VS Code.
111+
And \r \n are not interpreted as line endings, at least by VS Code.
112+
So we cannot make a newline break here. }
113+
SErrorPrefix = '%s(%d,%d): ';
114+
115+
{ Return prefix for error message describing position (line, column)
116+
from ECodeToolError, if any. }
117+
function PositionForErrorPrefix(const E: ECodeToolError): String;
118+
119+
function PosSet(const Pos: TCodeXYPosition): Boolean;
120+
begin
121+
Result := (Pos.X <> 0) and (Pos.Y <> 0);
122+
end;
123+
124+
function PosToStr(const Pos: TCodeXYPosition): String;
125+
var
126+
CodeFileName: String;
127+
begin
128+
if Pos.Code <> nil then
129+
CodeFileName := ExtractFileName(Pos.Code.Filename)
130+
else
131+
CodeFileName := '';
132+
Result := Format(SErrorPrefix, [CodeFileName, Pos.Y, Pos.X]);
133+
end;
134+
135+
begin
136+
if E.Sender <> nil then
137+
begin
138+
if PosSet(E.Sender.ErrorNicePosition) then
139+
Exit(PosToStr(E.Sender.ErrorNicePosition));
140+
if PosSet(E.Sender.ErrorPosition) then
141+
Exit(PosToStr(E.Sender.ErrorPosition));
142+
end;
143+
Result := '';
144+
end;
145+
146+
function PositionForErrorPrefix(const CodeToolBoss: TCodeToolManager): String;
147+
var
148+
CodeFileName: String;
149+
begin
150+
Result := '';
151+
if (CodeToolBoss.ErrorLine <> 0) and
152+
(CodeToolBoss.ErrorColumn <> 0) then
153+
begin
154+
if CodeToolBoss.ErrorCode <> nil then
155+
CodeFileName := ExtractFileName(CodeToolBoss.ErrorCode.Filename)
156+
else
157+
CodeFileName := '';
158+
Result := Format(SErrorPrefix, [
159+
CodeFileName,
160+
CodeToolBoss.ErrorLine,
161+
CodeToolBoss.ErrorColumn
162+
]);
163+
end;
164+
end;
97165

98166
end.
99167

0 commit comments

Comments
 (0)