Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix various QA issues #54

Merged
merged 5 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ sonar.projectKey=delphilint-client
sonar.projectName=DelphiLint Client
sonar.projectBaseDir=.

sonar.sourceEncoding=utf-8
sonar.sources=source
sonar.tests=test
sonar.exclusions=**/__history/**,**/__recovery/**,**/target/**
4 changes: 2 additions & 2 deletions client/source/DelphiLint.Analyzer.pas
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ function TAnalyzerImpl.FilterNonProjectFiles(const InFiles: TArray<string>; cons
constructor TAnalyzerImpl.Create;
begin
inherited;
FActiveIssues := TObjectDictionary<string, TList<ILiveIssue>>.Create;
FActiveIssues := TObjectDictionary<string, TList<ILiveIssue>>.Create([doOwnsValues]);
FCurrentAnalysis := nil;
FFileAnalyses := TDictionary<string, TFileAnalysisHistory>.Create;
FOnAnalysisStateChanged := TEventNotifier<TAnalysisStateChangeContext>.Create;
FRules := TObjectDictionary<string, TRule>.Create;
FRules := TObjectDictionary<string, TRule>.Create([doOwnsValues]);
FServerThread := TLintServerThread.Create;
FServerThread.FreeOnTerminate := False;
end;
Expand Down
78 changes: 42 additions & 36 deletions client/source/DelphiLint.IssueActions.pas
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,38 @@ procedure CalculateQuickFixReflowMetrics(

procedure ReflowQuickFixEdits(TextEdits: TList<TLiveTextEdit>; EditIndex: Integer);
var
AppliedEdit: TLiveTextEdit;
I: Integer;
LinesAdded: Integer;
ColumnsAdded: Integer;
SuccessorEdit: TLiveTextEdit;
AppliedLine: Integer;
AppliedLineOffset: Integer;
LineAfter: Boolean;
PositionAfter: Boolean;
LinesAdded: Integer;
ColumnsAdded: Integer;

procedure OffsetPosition(var TargetLine: Integer; var TargetOffset: Integer);
var
LineAfter: Boolean;
PositionAfter: Boolean;
begin
LineAfter := TargetLine > AppliedLine;
PositionAfter := LineAfter or ((TargetLine = AppliedLine) and (TargetOffset >= AppliedLineOffset));

if PositionAfter then begin
if not LineAfter then begin
// This pos starts after the applied edit on the same line, offset column
TargetOffset := TargetOffset + ColumnsAdded;
end;

// This pos starts after the applied edit, offset line
TargetLine := TargetLine + LinesAdded;
end;
end;

var
AppliedEdit: TLiveTextEdit;
CurrentEdit: TLiveTextEdit;
I: Integer;
StartLine: Integer;
StartLineOffset: Integer;
EndLine: Integer;
EndLineOffset: Integer;
begin
AppliedEdit := TextEdits[EditIndex];
AppliedLine := AppliedEdit.RelativeEndLine;
Expand All @@ -313,42 +336,25 @@ procedure ReflowQuickFixEdits(TextEdits: TList<TLiveTextEdit>; EditIndex: Intege
end;

for I := EditIndex + 1 to TextEdits.Count - 1 do begin
SuccessorEdit := TextEdits[I];
CurrentEdit := TextEdits[I];

if SuccessorEdit.RelativeEndLine < AppliedLine then begin
if CurrentEdit.RelativeEndLine < AppliedLine then begin
// Higher edits don't need to be offset
Continue;
end;

LineAfter := SuccessorEdit.RelativeStartLine > AppliedLine;
PositionAfter := LineAfter or (
(SuccessorEdit.RelativeStartLine = AppliedLine) and (SuccessorEdit.StartLineOffset >= AppliedLineOffset)
);

if PositionAfter then begin
if not LineAfter then begin
// This edit starts after the applied edit on the same line, offset column
SuccessorEdit.StartLineOffset := SuccessorEdit.StartLineOffset + ColumnsAdded;
end;

// This edit starts after the applied edit, offset line
SuccessorEdit.RelativeStartLine := SuccessorEdit.RelativeStartLine + LinesAdded;
end;

LineAfter := SuccessorEdit.RelativeEndLine > AppliedLine;
PositionAfter := LineAfter or (
(SuccessorEdit.RelativeEndLine = AppliedLine) and (SuccessorEdit.EndLineOffset >= AppliedLineOffset)
);
StartLine := CurrentEdit.RelativeStartLine;
StartLineOffset := CurrentEdit.StartLineOffset;
EndLine := CurrentEdit.RelativeEndLine;
EndLineOffset := CurrentEdit.EndLineOffset;

if PositionAfter then begin
if not LineAfter then begin
// This edit after the applied edit on the same line, offset column
SuccessorEdit.EndLineOffset := SuccessorEdit.EndLineOffset + ColumnsAdded;
end;
OffsetPosition(StartLine, StartLineOffset);
OffsetPosition(EndLine, EndLineOffset);

// This edit ends after the applied edit, offset line
SuccessorEdit.RelativeEndLine := SuccessorEdit.RelativeEndLine + LinesAdded;
end;
CurrentEdit.RelativeStartLine := StartLine;
CurrentEdit.StartLineOffset := StartLineOffset;
CurrentEdit.RelativeEndLine := EndLine;
CurrentEdit.EndLineOffset := EndLineOffset;
end;
end;

Expand Down
2 changes: 1 addition & 1 deletion client/source/DelphiLint.Properties.pas
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ TBoolPropField = class(TPropFieldBase)
procedure Load(IniFile: TIniFile); override;
end;

TPropertiesFile = class(TInterfacedObject)
TPropertiesFile = class(TObject)
private
FPath: string;
FFields: TArray<TPropFieldBase>;
Expand Down
4 changes: 2 additions & 2 deletions client/source/DelphiLint.Resources.pas
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ constructor TLintResources.Create;
begin
inherited;

FLoadedPngs := TObjectDictionary<string, TPngImage>.Create;
FLoadedBitmaps := TObjectDictionary<string, TBitmap>.Create;
FLoadedPngs := TObjectDictionary<string, TPngImage>.Create([doOwnsValues]);
FLoadedBitmaps := TObjectDictionary<string, TBitmap>.Create([doOwnsValues]);
FLoadedStrings := TDictionary<string, string>.Create;
end;

Expand Down
2 changes: 1 addition & 1 deletion client/source/DelphiLint.Server.pas
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ procedure TLintServer.OnRuleRetrieveResponse(
var
Pair: TJSONPair;
begin
Result := TObjectDictionary<string, TRule>.Create;
Result := TObjectDictionary<string, TRule>.Create([doOwnsValues]);
for Pair in Json do begin
Result.Add(Pair.JsonString.Value, TRule.CreateFromJson(TJSONObject(Pair.JsonValue)));
end;
Expand Down
60 changes: 41 additions & 19 deletions client/test/DelphiLintTest.MockContext.pas
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ TMockIDE = class(TObject)
FActions: TList<TCustomAction>;
FIDEInsightActions: TList<TCustomActionList>;
FEditorNotifiers: TDictionary<Integer, IIDEEditorHandler>;
FMenus: TObjectDictionary<string, TMenuItem>;
FMenus: TDictionary<string, TMenuItem>;
FPluginInfos: TObjectDictionary<Integer, TMockPluginInfo>;
FPluginTitle: string;
FPluginIcon: TBitmap;
Expand All @@ -261,7 +261,7 @@ TMockIDE = class(TObject)
property Actions: TList<TCustomAction> read FActions;
property IDEInsightActions: TList<TCustomActionList> read FIDEInsightActions;
property EditorNotifiers: TDictionary<Integer, IIDEEditorHandler> read FEditorNotifiers;
property Menus: TObjectDictionary<string, TMenuItem> read FMenus;
property Menus: TDictionary<string, TMenuItem> read FMenus;
property PluginInfos: TObjectDictionary<Integer, TMockPluginInfo> read FPluginInfos;

property PluginTitle: string read FPluginTitle write FPluginTitle;
Expand Down Expand Up @@ -362,15 +362,18 @@ TMockIDEServices = class(THookedObject<TIDEServicesCallType>, IIDEServices)
property IDE: TMockIDE read FIDE;
end;

TProjectOptionsProvider = reference to function: TLintProjectOptions;

TMockLintContext = class(TInterfacedObject, ILintContext)
private
FAnalyzer: IAnalyzer;
FIDEServices: IIDEServices;
FPlugin: IPlugin;
FSettings: TLintSettings;
FProjectOptionsList: TObjectDictionary<string, TLintProjectOptions>;
FProjectOptionsProviders: TDictionary<string, TProjectOptionsProvider>;
FSetupValid: Boolean;
FTempSettingsFile: string;
FInited: Boolean;

procedure Init;
procedure Deinit;
Expand All @@ -387,7 +390,7 @@ TMockLintContext = class(TInterfacedObject, ILintContext)
procedure MockPlugin(Plugin: IPlugin);
procedure MockSettings; overload;
procedure MockSettings(Settings: TLintSettings); overload;
procedure MockProjectOptions(ProjectFile: string; ProjectOptions: TLintProjectOptions);
procedure MockProjectOptions(ProjectFile: string; Provider: TProjectOptionsProvider);
procedure MockInvalidSetup;
procedure Reset;

Expand Down Expand Up @@ -429,8 +432,8 @@ constructor TMockAnalyzer.Create;

FFileHistories := TDictionary<string, TFileAnalysisHistory>.Create;
FFileStatuses := TDictionary<string, TFileAnalysisStatus>.Create;
FIssues := TObjectDictionary<string, TDictionary<Integer, ILiveIssue>>.Create;
FRules := TObjectDictionary<string, TRule>.Create;
FIssues := TObjectDictionary<string, TDictionary<Integer, ILiveIssue>>.Create([doOwnsValues]);
FRules := TObjectDictionary<string, TRule>.Create([doOwnsValues]);
end;

//______________________________________________________________________________________________________________________
Expand Down Expand Up @@ -574,7 +577,7 @@ procedure TMockAnalyzer.MockFileIssue(Path: string; Line: Integer; Issue: ILiveI
Path := NormalizePath(Path);
FIssues.AddOrSetValue(
Path,
TObjectDictionary<Integer, ILiveIssue>.Create([TPair<Integer, ILiveIssue>.Create(Line, Issue)]));
TDictionary<Integer, ILiveIssue>.Create([TPair<Integer, ILiveIssue>.Create(Line, Issue)]));
end;

//______________________________________________________________________________________________________________________
Expand All @@ -584,7 +587,7 @@ procedure TMockAnalyzer.MockFileIssues(Path: string; Issues: TArray<ILiveIssue>)
I: Integer;
begin
Path := NormalizePath(Path);
FIssues.AddOrSetValue(Path, TObjectDictionary<Integer, ILiveIssue>.Create);
FIssues.AddOrSetValue(Path, TDictionary<Integer, ILiveIssue>.Create);

for I := 0 to Length(Issues) - 1 do begin
FIssues[Path].AddOrSetValue(Issues[I].StartLine, Issues[I]);
Expand Down Expand Up @@ -656,24 +659,35 @@ destructor TMockLintContext.Destroy;

procedure TMockLintContext.Init;
begin
FProjectOptionsList := TObjectDictionary<string, TLintProjectOptions>.Create;
if FInited then begin
Exit;
end;

FProjectOptionsProviders := TDictionary<string, TProjectOptionsProvider>.Create;
FSetupValid := True;
FInited := True;
end;

//______________________________________________________________________________________________________________________

procedure TMockLintContext.Deinit;
begin
if not FInited then begin
Exit;
end;

FAnalyzer := nil;
FPlugin := nil;
FreeAndNil(FSettings);
FIDEServices := nil;
FreeAndNil(FProjectOptionsList);
FreeAndNil(FProjectOptionsProviders);

if FTempSettingsFile <> '' then begin
TFile.Delete(FTempSettingsFile);
FTempSettingsFile := '';
end;

FInited := False;
end;

//______________________________________________________________________________________________________________________
Expand Down Expand Up @@ -714,11 +728,11 @@ function TMockLintContext.GetPlugin: IPlugin;
function TMockLintContext.GetProjectOptions(ProjectFile: string): TLintProjectOptions;
begin
ProjectFile := NormalizePath(ProjectFile);
if not FProjectOptionsList.ContainsKey(ProjectFile) then begin
if not FProjectOptionsProviders.ContainsKey(ProjectFile) then begin
raise EMockError.CreateFmt('Project options for file ''%s'' have not been mocked', [ProjectFile]);
end;

Result := FProjectOptionsList[ProjectFile];
Result := FProjectOptionsProviders[ProjectFile]();
end;

//______________________________________________________________________________________________________________________
Expand Down Expand Up @@ -762,9 +776,9 @@ procedure TMockLintContext.MockPlugin(Plugin: IPlugin);

//______________________________________________________________________________________________________________________

procedure TMockLintContext.MockProjectOptions(ProjectFile: string; ProjectOptions: TLintProjectOptions);
procedure TMockLintContext.MockProjectOptions(ProjectFile: string; Provider: TProjectOptionsProvider);
begin
FProjectOptionsList.AddOrSetValue(NormalizePath(ProjectFile), ProjectOptions);
FProjectOptionsProviders.AddOrSetValue(NormalizePath(ProjectFile), Provider);
end;

//______________________________________________________________________________________________________________________
Expand Down Expand Up @@ -867,7 +881,7 @@ constructor TMockIDEServices.Create;
FMacros := TDictionary<string, string>.Create;
FStyleColors := TDictionary<TStyleColor, TColor>.Create;
FSystemColors := TDictionary<TColor, TColor>.Create;
FToolBars := TObjectDictionary<string, TToolBar>.Create;
FToolBars := TObjectDictionary<string, TToolBar>.Create([doOwnsValues]);
FModules := TList<IIDEModule>.Create;
end;

Expand Down Expand Up @@ -931,7 +945,8 @@ procedure TMockIDEServices.AddPluginBitmap(const Caption: string; Image: TBitmap
const LicenseStatus: string; const Version: string);
begin
FIDE.PluginTitle := Caption;
FIDE.PluginIcon := Image;
FIDE.PluginIcon := TBitmap.Create;
FIDE.PluginIcon.Assign(Image);
FIDE.PluginLicenseStatus := LicenseStatus;
FIDE.PluginVersion := Version;
FIDE.PluginRegistered := not IsUnregistered;
Expand All @@ -940,10 +955,15 @@ procedure TMockIDEServices.AddPluginBitmap(const Caption: string; Image: TBitmap
//______________________________________________________________________________________________________________________

function TMockIDEServices.AddPluginInfo(const Title: string; const Description: string; Image: TBitmap): Integer;
var
ImageCopy: TBitmap;
begin
Result := FNextId;
Inc(FNextId);
FIDE.PluginInfos.Add(Result, TMockPluginInfo.Create(Title, Description, Image));

ImageCopy := TBitmap.Create;
ImageCopy.Assign(Image);
FIDE.PluginInfos.Add(Result, TMockPluginInfo.Create(Title, Description, ImageCopy));
end;

//______________________________________________________________________________________________________________________
Expand Down Expand Up @@ -1137,10 +1157,12 @@ constructor TMockIDE.Create;
FActions := TList<TCustomAction>.Create;
FIDEInsightActions := TList<TCustomActionList>.Create;
FEditorNotifiers := TDictionary<Integer, IIDEEditorHandler>.Create;
FMenus := TObjectDictionary<string, TMenuItem>.Create;
FPluginInfos := TObjectDictionary<Integer, TMockPluginInfo>.Create;
FMenus := TDictionary<string, TMenuItem>.Create;
FPluginInfos := TObjectDictionary<Integer, TMockPluginInfo>.Create([doOwnsValues]);
end;

//______________________________________________________________________________________________________________________

destructor TMockIDE.Destroy;
begin
FreeAndNil(FActions);
Expand Down
9 changes: 0 additions & 9 deletions client/test/DelphiLintTest.Plugin.pas
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ TIDEPluginTest = class(TObject)
procedure MockAllToolBars(IDEServices: TMockIDEServices);
procedure BuildMockedContext(out IDEServices: TMockIDEServices);
public
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;

Expand Down Expand Up @@ -76,13 +74,6 @@ implementation

//______________________________________________________________________________________________________________________

procedure TIDEPluginTest.Setup;
begin
MockContext.Reset;
end;

//______________________________________________________________________________________________________________________

procedure TIDEPluginTest.TearDown;
begin
MockContext.Reset;
Expand Down
11 changes: 7 additions & 4 deletions client/test/DelphiLintTest.Utils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,15 @@ procedure TUtilsTest.TestTryGetProjectDirectoryWithReadOptionsOnGetsAbsoluteAnal
IDEServices: TMockIDEServices;
Project: TMockProject;
ProjectDir: string;
ProjectOptions: TLintProjectOptions;
begin
MockIDEServices(IDEServices);
ProjectOptions := TLintProjectOptions.Create(CProjectOptionsFile, True);
MockContext.MockProjectOptions(CProjectFile, ProjectOptions);
ProjectOptions.AnalysisBaseDir := CProjectDirRelative;
MockContext.MockProjectOptions(
CProjectFile,
function: TLintProjectOptions
begin
Result := TLintProjectOptions.Create(CProjectOptionsFile, True);
Result.AnalysisBaseDir := CProjectDirRelative;
end);

Project := TMockProject.Create;
IDEServices.MockActiveProject(Project);
Expand Down
Loading
Loading