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

TSynGeneralSyn.SetKeyWords mutates the list it gets in parameter #180

Open
EricGrange opened this issue Jan 11, 2022 · 2 comments
Open

TSynGeneralSyn.SetKeyWords mutates the list it gets in parameter #180

EricGrange opened this issue Jan 11, 2022 · 2 comments

Comments

@EricGrange
Copy link
Contributor

The implementation of TSynGeneralSyn.SetKeyWords first turns all the strings in its TUnicodeStrings parameter, then forces it to be Sorted (see snippet below) before the method exits.

Does anyone know if there is a particular rationale for it ?

Otherwise it seems to be that FKeyWords should be where the uppercase is applied.

procedure TSynGeneralSyn.SetKeyWords(const Value: TUnicodeStrings);
var
  i: Integer;
begin
  if Value <> nil then
  begin
    Value.BeginUpdate;
    for i := 0 to Value.Count - 1 do
      Value[i] := SynWideUpperCase(Value[i]);
    Value.EndUpdate;
  end;

  TUnicodeStringList(FKeyWords).Sorted := False;
  FKeyWords.Assign(Value);
  TUnicodeStringList(FKeyWords).Sorted := True;

  DefHighLightChange(nil);
end;
@pyscripter
Copy link
Contributor

I guess because it is faster to add strings to an unsorted TStrings and then sort than to add them to a sorted list.

@EricGrange
Copy link
Contributor Author

Yes, but why mutate the list passed in parameter ?

Also it's the parameter list that is set to unsorted, not the destination list, and keywords are rarely changed in an highlighter (usually once at initialization).

AFAICT this would be similar without affecting the parameter list:

procedure TSynGeneralSyn.SetKeyWords(const Value: TUnicodeStrings);
var
  i: Integer;
begin
  FKeyWords.BeginUpdate;
  FKeyWords.Clear;
  if Value <> nil then
  begin
    for i := 0 to Value.Count - 1 do
      FKeyWords.Add(SynWideUpperCase(Value[i]));
  end;
  FKeyWords.EndUpdate;

  DefHighLightChange(nil);
end;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants