ToDoList: Simplify and optimize. Fix function TextToSingleLine in LazStringUtils, now only used by ToDoList. Issue #41437.

This commit is contained in:
Juha 2025-03-28 21:15:52 +02:00
parent 9613485cf8
commit 3b0f60efcb
3 changed files with 27 additions and 33 deletions

View File

@ -1242,36 +1242,33 @@ end;
} }
function TextToSingleLine(const AText: string): string; function TextToSingleLine(const AText: string): string;
var var
str: string;
i, wstart, wlen: Integer; i, wstart, wlen: Integer;
begin begin
str := Trim(AText); Result := Trim(AText);
wstart := 0; wstart := 0;
wlen := 0;
i := 1; i := 1;
while i < Length(str) - 1 do while i <= Length(Result) do
begin begin
if (str[i] in [' ', #13, #10]) then if Result[i] in [' ', #13, #10] then
begin begin
if (wstart = 0) then if wstart = 0 then
begin begin
wstart := i; wstart := i;
wlen := 1; wlen := 0;
end else end else
Inc(wlen); Inc(wlen);
end else end else
begin begin
if wstart > 0 then if wstart > 0 then
begin begin
str[wstart] := ' '; Result[wstart] := ' ';
Delete(str, wstart+1, wlen-1); Delete(Result, wstart+1, wlen);
Dec(i, wlen-1); Dec(i, wlen);
wstart := 0; wstart := 0;
end; end;
end; end;
Inc(i); Inc(i);
end; end;
Result := str;
end; end;
function SwapCase(const S: String): String; function SwapCase(const S: String): String;

View File

@ -212,12 +212,12 @@ begin
CategoryLabel.Caption:=listToDoLCategory; CategoryLabel.Caption:=listToDoLCategory;
grpboxToDoType.Caption:=lisToDoToDoType; grpboxToDoType.Caption:=lisToDoToDoType;
grpboxToDoType.Tag:=Ord(tdToDo); grpboxToDoType.Tag:=Ord(tdToDo);
rdoToDo.Tag := Ord(tdToDo); rdoToDo.Tag:=Ord(tdToDo);
rdoDone.Tag:=Ord(tdDone); rdoDone.Tag:=Ord(tdDone);
rdoNote.Tag:=Ord(tdNote); rdoNote.Tag:=Ord(tdNote);
chkAlternateTokens.Caption:=lisAlternateTokens; chkAlternateTokens.Caption:=lisAlternateTokens;
chkAlternateTokens.Hint:=lisAlternateTokensHint; chkAlternateTokens.Hint:=lisAlternateTokensHint;
XMLPropStorage.FileName := Concat(AppendPathDelim(LazarusIDE.GetPrimaryConfigPath), XMLPropStorage.FileName:=Concat(AppendPathDelim(LazarusIDE.GetPrimaryConfigPath),
DefaultTodoListCfgFile); DefaultTodoListCfgFile);
XMLPropStorage.Active := True; XMLPropStorage.Active := True;
end; end;

View File

@ -238,19 +238,22 @@ end;
procedure TTLScannedFile.CreateToDoItem(const aStartComment, aEndComment: string; procedure TTLScannedFile.CreateToDoItem(const aStartComment, aEndComment: string;
aLineNumber: Integer); aLineNumber: Integer);
var var
lParsingString, TheToken: string; TheToken: string;
lTokenFound: boolean; lTokenFound: boolean;
lTodoType, lFoundToDoType: TToDoType; lTodoType, lFoundToDoType: TToDoType;
lTokenStyle, lFoundTokenStyle: TTokenStyle; lTokenStyle, lFoundTokenStyle: TTokenStyle;
NewToDoItem: TTodoItem; NewToDoItem: TTodoItem;
begin begin
//DebugLn(['TTLScannedFile.CreateToDoItem FileName=',FRealFilename,' LineNumber=',aLineNumber]); //DebugLn(['TTLScannedFile.CreateToDoItem FileName=',FRealFilename,
lParsingString := TextToSingleLine(FCommentStr); // ', LineNumber=',aLineNumber, ', FCommentStr=',FCommentStr]);
// Remove the beginning comment chars from input string // Remove beginning and ending comment characters from the string
if aStartComment <> '' then if aStartComment <> '' then
Delete(lParsingString, 1, Length(aStartComment)); Delete(FCommentStr, 1, Length(aStartComment));
// Remove leading and trailing blanks from input if aEndComment <> '' then begin
lParsingString := Trim(lParsingString); Assert(LazEndsStr(aEndComment, FCommentStr), 'TTLScannedFile.CreateToDoItem: No comment end.');
SetLength(FCommentStr, Length(FCommentStr)-Length(aEndComment));
end;
FCommentStr := TextToSingleLine(FCommentStr);
// Determine Token and Style // Determine Token and Style
lTokenFound := False; lTokenFound := False;
@ -260,13 +263,11 @@ begin
for lTodoType := Low(TToDoType) to High(TToDoType) do for lTodoType := Low(TToDoType) to High(TToDoType) do
begin begin
TheToken := TODO_TOKENS[lTokenStyle,lTodoType]; TheToken := TODO_TOKENS[lTokenStyle,lTodoType];
if LazStartsText(TheToken, lParsingString) then if LazStartsText(TheToken, FCommentStr) then
begin begin
if (Length(lParsingString)=Length(TheToken)) // Don't match with 'ToDoX' if (Length(FCommentStr)=Length(TheToken)) // Don't match with 'ToDoX'
or (lParsingString[Length(TheToken)+1] in [#9,#10,#13,' ',':']) or (FCommentStr[Length(TheToken)+1] in [#9,' ',':']) then
or ((aEndComment <> '') and begin
(Copy(lParsingString,Length(TheToken)+1,Length(aEndComment))=aEndComment))
then begin
lTokenFound := True; // Token match lTokenFound := True; // Token match
lFoundToDoType := lTodoType; lFoundToDoType := lTodoType;
lFoundTokenStyle := lTokenStyle; lFoundTokenStyle := lTokenStyle;
@ -279,18 +280,14 @@ begin
if Not lTokenFound then if Not lTokenFound then
Exit; // Not a Todo/Done item, leave Exit; // Not a Todo/Done item, leave
// Remove the ending comment chars from input string
if (aEndComment <> '') and LazEndsStr(aEndComment, lParsingString) then
SetLength(lParsingString, Length(lParsingString)-Length(aEndComment));
// Remove the ToDo token // Remove the ToDo token
Assert(TheToken=TODO_TOKENS[lFoundTokenStyle,lFoundToDoType], 'TTLScannedFile.CreateToDoItem: TheToken'); Assert(TheToken=TODO_TOKENS[lFoundTokenStyle,lFoundToDoType], 'TTLScannedFile.CreateToDoItem: TheToken');
Delete(lParsingString, 1, Length(TheToken)); Delete(FCommentStr, 1, Length(TheToken));
lParsingString := Trim(lParsingString); FCommentStr := TrimLeft(FCommentStr);
// Require a colon with plain "done" but not with "#done". Prevent false positives. // Require a colon with plain "done" but not with "#done". Prevent false positives.
NewToDoItem:=TTodoItem.Create; NewToDoItem:=TTodoItem.Create;
if NewToDoItem.Parse(lParsingString, lFoundTokenStyle=tsAlternate) then if NewToDoItem.Parse(FCommentStr, lFoundTokenStyle=tsAlternate) then
begin begin
NewToDoItem.ToDoType := lFoundToDoType; NewToDoItem.ToDoType := lFoundToDoType;
NewToDoItem.TokenStyle := lFoundTokenStyle; NewToDoItem.TokenStyle := lFoundTokenStyle;