Skip to content

Commit

Permalink
Show filename, line, column in error messages coming from parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
michaliskambi committed Nov 8, 2024
1 parent fd01632 commit d698768
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
8 changes: 2 additions & 6 deletions server/utextdocument.pas
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,7 @@ procedure GetCompletionRecords(
if not CodeToolBoss.GatherIdentifiers(Code, X, Y) then
raise ERpcError.Create(
jsrpcRequestFailed,
Format('Line %d: %s', [
CodeToolBoss.ErrorLine,
CodeToolBoss.ErrorMessage
])
);
PositionForErrorPrefix(CodeToolBoss) + CodeToolBoss.ErrorMessage);

Count := CodeToolBoss.IdentifierList.GetFilteredCount;

Expand Down Expand Up @@ -992,7 +988,7 @@ procedure TextDocument_JumpTo(
// has id 20170421200105 so we then do not show message window in vscode
if E.Id <> 20170421200105 then
begin
ShowErrorMessage(Rpc, E.Message);
ShowErrorMessage(Rpc, PositionForErrorPrefix(E) + E.Message);
end;
end;
{ ELinkScannerError is raised from FindDeclaration e.g. when include file is missing.
Expand Down
70 changes: 69 additions & 1 deletion server/uutils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,25 @@

interface

uses CustomCodeTool, CodeToolManager, CodeCache;

function MergePaths(Paths: array of string): string;
function GetConfigDirForApp(AppName, Vendor: string; Global: Boolean): string;
function URIToFileNameEasy(const UriStr: String): String;

{ Return prefix for error message describing filename, line, column
from ECodeToolError, if any. }
function PositionForErrorPrefix(const E: ECodeToolError): String; overload;

{ Return prefix for error message describing filename, line, column
from TCodeToolManager, if any. }
function PositionForErrorPrefix(const CodeToolBoss: TCodeToolManager): String; overload;

implementation

uses
SysUtils, URIParser, ujsonrpc;
SysUtils, URIParser,
ujsonrpc;

function MergePaths(Paths: array of string): string;
var
Expand Down Expand Up @@ -94,6 +105,63 @@ function URIToFileNameEasy(const UriStr: String): String;
);
end;

const
{ Error prefix to display filename (may be ''), line, column.
Note: line endings (#10, #13 or both) are ignored inside this, at least by VS Code.
And \r \n are not interpreted as line endings, at least by VS Code.
So we cannot make a newline break here. }
SErrorPrefix = '%s(%d,%d): ';

{ Return prefix for error message describing position (line, column)
from ECodeToolError, if any. }
function PositionForErrorPrefix(const E: ECodeToolError): String;

function PosSet(const Pos: TCodeXYPosition): Boolean;
begin
Result := (Pos.X <> 0) and (Pos.Y <> 0);
end;

function PosToStr(const Pos: TCodeXYPosition): String;
var
CodeFileName: String;
begin
if Pos.Code <> nil then
CodeFileName := ExtractFileName(Pos.Code.Filename)
else
CodeFileName := '';
Result := Format(SErrorPrefix, [CodeFileName, Pos.Y, Pos.X]);
end;

begin
if E.Sender <> nil then
begin
if PosSet(E.Sender.ErrorNicePosition) then
Exit(PosToStr(E.Sender.ErrorNicePosition));
if PosSet(E.Sender.ErrorPosition) then
Exit(PosToStr(E.Sender.ErrorPosition));
end;
Result := '';
end;

function PositionForErrorPrefix(const CodeToolBoss: TCodeToolManager): String;
var
CodeFileName: String;
begin
Result := '';
if (CodeToolBoss.ErrorLine <> 0) and
(CodeToolBoss.ErrorColumn <> 0) then
begin
if CodeToolBoss.ErrorCode <> nil then
CodeFileName := ExtractFileName(CodeToolBoss.ErrorCode.Filename)
else
CodeFileName := '';
Result := Format(SErrorPrefix, [
CodeFileName,
CodeToolBoss.ErrorLine,
CodeToolBoss.ErrorColumn
]);
end;
end;

end.

0 comments on commit d698768

Please sign in to comment.