extended MakeResourceString function to convert parts of string constants

git-svn-id: trunk@7358 -
This commit is contained in:
mattias 2005-07-15 16:25:39 +00:00
parent 42f6cd4d0e
commit 772ebf4265
5 changed files with 164 additions and 95 deletions

View File

@ -324,11 +324,12 @@ type
function CreateIdentifierFromStringConst(
StartCode: TCodeBuffer; StartX, StartY: integer;
EndCode: TCodeBuffer; EndX, EndY: integer;
var Identifier: string; MaxLen: integer): boolean;
out Identifier: string; MaxLen: integer): boolean;
function StringConstToFormatString(
StartCode: TCodeBuffer; StartX, StartY: integer;
EndCode: TCodeBuffer; EndX, EndY: integer;
var FormatStringConstant, FormatParameters: string): boolean;
out FormatStringConstant, FormatParameters: string;
out StartInStringConst, EndInStringConst: boolean): boolean;
function GatherResourceStringsWithValue(SectionCode: TCodeBuffer;
SectionX, SectionY: integer; const StringValue: string;
CodePositions: TCodeXYPositions): boolean;
@ -1630,7 +1631,7 @@ end;
function TCodeToolManager.CreateIdentifierFromStringConst(
StartCode: TCodeBuffer; StartX, StartY: integer;
EndCode: TCodeBuffer; EndX, EndY: integer;
var Identifier: string; MaxLen: integer): boolean;
out Identifier: string; MaxLen: integer): boolean;
var
StartCursorPos, EndCursorPos: TCodeXYPosition;
begin
@ -1657,7 +1658,8 @@ end;
function TCodeToolManager.StringConstToFormatString(
StartCode: TCodeBuffer; StartX, StartY: integer;
EndCode: TCodeBuffer; EndX, EndY: integer;
var FormatStringConstant, FormatParameters: string): boolean;
out FormatStringConstant, FormatParameters: string;
out StartInStringConst, EndInStringConst: boolean): boolean;
var
StartCursorPos, EndCursorPos: TCodeXYPosition;
begin
@ -1674,7 +1676,8 @@ begin
EndCursorPos.Code:=EndCode;
try
Result:=FCurCodeTool.StringConstToFormatString(
StartCursorPos,EndCursorPos,FormatStringConstant,FormatParameters);
StartCursorPos,EndCursorPos,FormatStringConstant,FormatParameters,
StartInStringConst,EndInStringConst);
except
on e: Exception do HandleException(e);
end;

View File

@ -57,7 +57,7 @@ type
function ExtractCode(StartPos, EndPos: integer;
Attr: TProcHeadAttributes): string;
function ExtractIdentCharsFromStringConstant(
StartPos, MaxLen: integer): string;
StartPos, MinPos, MaxPos, MaxLen: integer): string;
function ReadStringConstantValue(StartPos: integer): string;
// properties
@ -864,11 +864,12 @@ begin
end;
function TPascalReaderTool.ExtractIdentCharsFromStringConstant(StartPos,
MaxLen: integer): string;
MinPos, MaxPos, MaxLen: integer): string;
var
APos: Integer;
IdentStartPos: Integer;
IdentStr: String;
IdentEndPos: LongInt;
begin
Result:='';
APos:=StartPos;
@ -888,17 +889,20 @@ begin
repeat
// read identifier chars
IdentStartPos:=APos;
while (IdentStartPos<SrcLen) and (IsIdentChar[Src[IdentStartPos]]) do
inc(IdentStartPos);
if IdentStartPos>APos then begin
if IdentStartPos-APos+length(Result)>MaxLen then
IdentStartPos:=APos+MaxLen-length(Result);
IdentStr:=copy(Src,APos,IdentStartPos-APos);
if (Result<>'') and (IdentStr<>'') then
while (APos<SrcLen) and (IsIdentChar[Src[APos]]) do
inc(APos);
IdentEndPos:=APos;
if IdentStartPos<MinPos then IdentStartPos:=MinPos;
if IdentEndPos>MaxPos then IdentEndPos:=MaxPos;
if (IdentEndPos>IdentStartPos) then begin
if IdentEndPos-IdentStartPos+length(Result)>MaxLen then
IdentEndPos:=IdentStartPos+MaxLen-length(Result);
IdentStr:=copy(Src,IdentStartPos,IdentEndPos-IdentStartPos);
if (IdentStr<>'') then begin
IdentStr[1]:=UpChars[IdentStr[1]];
Result:=Result+IdentStr;
Result:=Result+IdentStr;
end;
end;
APos:=IdentStartPos;
// skip non identifier chars
while (APos<SrcLen) and (Src[APos]<>'''')
and (not IsIdentChar[Src[APos]])

View File

@ -215,7 +215,10 @@ type
const NewCode: string;
SourceChangeCache: TSourceChangeCache): boolean;
function GetStringConstAsFormatString(StartPos, EndPos: integer;
var FormatStringConstant,FormatParameters: string): boolean;
out FormatStringConstant, FormatParameters: string;
out StartInStringConst, EndInStringConst: boolean): boolean;
function GetStringConstAsFormatString(StartPos, EndPos: integer;
out FormatStringConstant, FormatParameters: string): boolean;
// resource strings
function GatherResourceStringSections(const CursorPos: TCodeXYPosition;
@ -238,10 +241,11 @@ type
SourceChangeCache: TSourceChangeCache): boolean;
function CreateIdentifierFromStringConst(
const StartCursorPos, EndCursorPos: TCodeXYPosition;
var Identifier: string; MaxLen: integer): boolean;
out Identifier: string; MaxLen: integer): boolean;
function StringConstToFormatString(
const StartCursorPos, EndCursorPos: TCodeXYPosition;
var FormatStringConstant,FormatParameters: string): boolean;
out FormatStringConstant,FormatParameters: string;
out StartInStringConst, EndInStringConst: boolean): boolean;
// register procedure
function HasInterfaceRegisterProc(var HasRegisterProc: boolean): boolean;
@ -2339,7 +2343,8 @@ begin
ReadNextAtom;
if AtomIsIdentifier(false) then begin
CurIdentNode:=
IdentTree.FindKey(@Src[CurPos.StartPos], TListSortCompare(@CompareIdentifiers));
IdentTree.FindKey(@Src[CurPos.StartPos],
TListSortCompare(@CompareIdentifiers));
if CurIdentNode<>nil then begin
CurDiff:=CurPos.StartPos-CleanCursorPos;
if CurDiff<0 then CurDiff:=-CurDiff;
@ -2349,7 +2354,7 @@ begin
end;
end;
end;
until CurPos.EndPos>SrcLen
until CurPos.EndPos>SrcLen;
end;
function TStandardCodeTool.GetStringConstBounds(
@ -2552,8 +2557,8 @@ begin
end;
function TStandardCodeTool.GetStringConstAsFormatString(StartPos,
EndPos: integer; var FormatStringConstant, FormatParameters: string
): boolean;
EndPos: integer; out FormatStringConstant, FormatParameters: string;
out StartInStringConst, EndInStringConst: boolean): boolean;
{ Converts a string constant into the parameters for a Format call of the
system unit.
@ -2585,19 +2590,30 @@ function TStandardCodeTool.GetStringConstAsFormatString(StartPos,
var
APos: Integer;
CharConstStart: Integer;
InRange: Boolean;
begin
if (CurPos.StartPos<StartPos) and (CurPos.EndPos>StartPos) then
StartInStringConst:=true;
if (CurPos.StartPos<EndPos) and (CurPos.EndPos>EndPos) then
EndInStringConst:=true;
APos:=CurPos.StartPos;
while APos<EndPos do begin
InRange:=(APos>=StartPos);
//debugln('ConvertStringConstant InRange=',dbgs(InRange),' Src[APos]=',Src[APos]);
if Src[APos]='''' then begin
// read string constant
inc(APos);
while APos<EndPos do begin
InRange:=(APos>=StartPos);
case Src[APos] of
'''':
if (APos<EndPos-1) and (Src[APos+1]='''') then begin
// a double ' means a single '
AddChar('''');
AddChar('''');
if InRange then begin
AddChar('''');
AddChar('''');
end;
inc(APos,2);
end else begin
// a single ' means end of string constant
@ -2606,19 +2622,22 @@ function TStandardCodeTool.GetStringConstAsFormatString(StartPos,
end;
'"':
begin
AddParameter('''"''');
if InRange then
AddParameter('''"''');
inc(APos);
end;
else
begin
// normal char
AddChar(Src[APos]);
if InRange then
AddChar(Src[APos]);
inc(APos);
end;
end;
end;
end else if Src[APos]='#' then begin
CharConstStart:=APos;
InRange:=(APos+1>=StartPos);
repeat
// read char constant
inc(APos);
@ -2634,7 +2653,8 @@ function TStandardCodeTool.GetStringConstAsFormatString(StartPos,
end;
end;
until (APos>=EndPos) or (Src[APos]<>'#');
AddParameter(CharConstStart,APos);
if InRange then
AddParameter(CharConstStart,APos);
end else
break;
end;
@ -2659,32 +2679,53 @@ function TStandardCodeTool.GetStringConstAsFormatString(StartPos,
if AtomIsStringConstant then UndoReadNextAtom;
end;
var
ANode: TCodeTreeNode;
CodePosInFront: LongInt;
begin
Result:=false;
// read string constants and convert it
FormatStringConstant:='';
FormatParameters:='';
MoveCursorToCleanPos(StartPos);
StartInStringConst:=false;
EndInStringConst:=false;
ANode:=FindDeepestNodeAtPos(StartPos,True);
CodePosInFront:=ANode.StartPos;
MoveCursorToCleanPos(CodePosInFront);
if EndPos>SrcLen then EndPos:=SrcLen+1;
repeat
ReadNextAtom;
if (CurPos.EndPos>EndPos) then break;
if AtomIsStringConstant then begin
// a string constant
ConvertStringConstant;
end else if AtomIsChar('+') then begin
// simply ignore
end else if (CurPos.Flag=cafRoundBracketOpen) or AtomIsIdentifier(false)
then begin
// add as parameter
ConvertOther;
end else
// string constant end
break;
//debugln('GetStringConstAsFormatString Atom=',GetAtom);
if (CurPos.StartPos>=EndPos) then break;
if CurPos.EndPos>StartPos then begin
//debugln('GetStringConstAsFormatString Parsing...');
if AtomIsStringConstant then begin
// a string constant
ConvertStringConstant;
end else if AtomIsChar('+') then begin
// simply ignore
end else if (CurPos.Flag=cafRoundBracketOpen) or AtomIsIdentifier(false)
then begin
// add as parameter
ConvertOther;
end else
// string constant end
break;
end;
until false;
Result:=FormatStringConstant<>'';
end;
function TStandardCodeTool.GetStringConstAsFormatString(StartPos,
EndPos: integer; out FormatStringConstant, FormatParameters: string
): boolean;
var
StartInStringConst, EndInStringConstant: boolean;
begin
Result:=GetStringConstAsFormatString(StartPos,EndPos,FormatStringConstant,
FormatParameters,StartInStringConst,EndInStringConstant);
end;
function TStandardCodeTool.GatherResourceStringSections(
const CursorPos: TCodeXYPosition; PositionList: TCodeXYPositions): boolean;
@ -2792,12 +2833,14 @@ begin
end;
function TStandardCodeTool.CreateIdentifierFromStringConst(const StartCursorPos,
EndCursorPos: TCodeXYPosition; var Identifier: string;
EndCursorPos: TCodeXYPosition; out Identifier: string;
MaxLen: integer): boolean;
var
StartPos, EndPos: integer;
Dummy: Integer;
IdentStr: String;
ANode: TCodeTreeNode;
CodePosInFront: LongInt;
begin
Result:=false;
if MaxLen<=0 then exit;
@ -2805,26 +2848,32 @@ begin
BuildTreeAndGetCleanPos(trAll,StartCursorPos,StartPos,[]);
Dummy:=CaretToCleanPos(EndCursorPos, EndPos);
if (Dummy<>0) and (Dummy<>-1) then exit;
ANode:=FindDeepestNodeAtPos(StartPos,True);
CodePosInFront:=ANode.StartPos;
// read string constants and extract identifier characters
Identifier:='';
MoveCursorToCleanPos(StartPos);
MoveCursorToCleanPos(CodePosInFront);
repeat
ReadNextAtom;
if CurPos.EndPos>EndPos then break;
//debugln('TStandardCodeTool.CreateIdentifierFromStringConst Atom=',GetAtom);
if (CurPos.StartPos>=EndPos) then break;
if AtomIsStringConstant then begin
IdentStr:=ExtractIdentCharsFromStringConstant(CurPos.StartPos,
MaxLen-length(Identifier));
if (Identifier<>'') and (IdentStr<>'') then
StartPos,EndPos,MaxLen-length(Identifier));
//debugln('TStandardCodeTool.CreateIdentifierFromStringConst IdentStr=',IdentStr);
if (IdentStr<>'') then begin
IdentStr[1]:=UpChars[IdentStr[1]];
Identifier:=Identifier+IdentStr;
Identifier:=Identifier+IdentStr;
end;
end;
until length(Identifier)>=MaxLen;
Result:=Identifier<>'';
end;
function TStandardCodeTool.StringConstToFormatString(const StartCursorPos,
EndCursorPos: TCodeXYPosition; var FormatStringConstant,
FormatParameters: string): boolean;
EndCursorPos: TCodeXYPosition;
out FormatStringConstant, FormatParameters: string;
out StartInStringConst, EndInStringConst: boolean): boolean;
var
StartPos,EndPos,Dummy: Integer;
begin
@ -2834,7 +2883,7 @@ begin
Dummy:=CaretToCleanPos(EndCursorPos, EndPos);
if (Dummy<>0) and (Dummy<>-1) then exit;
Result:=GetStringConstAsFormatString(StartPos,EndPos,FormatStringConstant,
FormatParameters);
FormatParameters,StartInStringConst,EndInStringConst);
end;
function TStandardCodeTool.HasInterfaceRegisterProc(var HasRegisterProc: boolean

View File

@ -10460,60 +10460,63 @@ begin
Result:=mrCancel;
if not BeginCodeTool(ActiveSrcEdit,ActiveUnitInfo,[]) then exit;
{$IFDEF IDE_DEBUG}
writeln('');
writeln('[TMainIDE.DoMakeResourceString] ************');
debugln('');
debugln('[TMainIDE.DoMakeResourceString] ************');
{$ENDIF}
// calculate start and end of expression in source
CursorCode:=ActiveUnitInfo.Source;
CursorXY:=ActiveSrcEdit.EditorComponent.LogicalCaretXY;
if CodeToolBoss.GetStringConstBounds(
if not CodeToolBoss.GetStringConstBounds(
CursorCode,CursorXY.X,CursorXY.Y,
StartCode,StartPos.X,StartPos.Y,
EndCode,EndPos.X,EndPos.Y,
true) then
begin
// the codetools have calculated the maximum bounds
if (StartCode=EndCode) and (CompareCaret(StartPos,EndPos)=0) then begin
MessageDlg(lisNoStringConstantFound,
Format(lisHintTheMakeResourcestringFunctionExpectsAStringCon, [#13]),
mtError,[mbCancel],0);
exit;
end;
// the user can shorten this range by selecting text
if (Trim(ActiveSrcEdit.EditorComponent.SelText)='') then begin
// the user has not selected text
// -> check if the string constant is in single file
// (replacing code that contains an $include directive is ambiguous)
if (StartCode<>ActiveUnitInfo.Source)
or (EndCode<>ActiveUnitInfo.Source)
then begin
MessageDlg(lisNoStringConstantFound, Format(
lisInvalidExpressionHintTheMakeResourcestringFunction, [#13]),
mtError,[mbCancel],0);
exit;
end;
end else begin
// the user has selected text
// -> check if the selection is only part of the maximum bounds
SelectedStartPos:=ActiveSrcEdit.EditorComponent.BlockBegin;
SelectedEndPos:=ActiveSrcEdit.EditorComponent.BlockEnd;
if (CompareCaret(SelectedStartPos,StartPos)>0)
or (CompareCaret(SelectedEndPos,EndPos)<0)
then begin
MessageDlg(lisSelectionExceedsStringConstant,
Format(lisHintTheMakeResourcestringFunctionExpectsAStringCon2, [#13]),
mtError,[mbCancel],0);
exit;
end;
StartPos:=SelectedStartPos;
EndPos:=SelectedEndPos;
end;
end else begin
DoJumpToCodeToolBossError;
exit;
end;
// the codetools have calculated the maximum bounds
if (StartCode=EndCode) and (CompareCaret(StartPos,EndPos)=0) then begin
MessageDlg(lisNoStringConstantFound,
Format(lisHintTheMakeResourcestringFunctionExpectsAStringCon, [#13]),
mtError,[mbCancel],0);
exit;
end;
// the user can shorten this range by selecting text
if (ActiveSrcEdit.EditorComponent.SelText='') then begin
// the user has not selected text
// -> check if the string constant is in single file
// (replacing code that contains an $include directive is ambiguous)
//debugln('TMainIDE.DoMakeResourceString user has not selected text');
if (StartCode<>ActiveUnitInfo.Source)
or (EndCode<>ActiveUnitInfo.Source)
then begin
MessageDlg(lisNoStringConstantFound, Format(
lisInvalidExpressionHintTheMakeResourcestringFunction, [#13]),
mtError,[mbCancel],0);
exit;
end;
end else begin
// the user has selected text
// -> check if the selection is only part of the maximum bounds
SelectedStartPos:=ActiveSrcEdit.EditorComponent.BlockBegin;
SelectedEndPos:=ActiveSrcEdit.EditorComponent.BlockEnd;
//debugln('TMainIDE.DoMakeResourceString user has selected text: Selected=',dbgs(SelectedStartPos),'-',dbgs(SelectedEndPos),' Maximum=',dbgs(StartPos),'-',dbgs(EndPos));
if (CompareCaret(SelectedStartPos,StartPos)>0)
or (CompareCaret(SelectedEndPos,EndPos)<0)
then begin
MessageDlg(lisSelectionExceedsStringConstant,
Format(lisHintTheMakeResourcestringFunctionExpectsAStringCon2, [#13]),
mtError,[mbCancel],0);
exit;
end;
StartPos:=SelectedStartPos;
EndPos:=SelectedEndPos;
end;
// gather all reachable resourcestring sections
//debugln('TMainIDE.DoMakeResourceString gather all reachable resourcestring sections ...');
if not CodeToolBoss.GatherResourceStringSections(
CursorCode,CursorXY.X,CursorXY.Y,nil)
then begin
@ -11810,6 +11813,9 @@ end.
{ =============================================================================
$Log$
Revision 1.885 2005/07/15 16:25:39 mattias
extended MakeResourceString function to convert parts of string constants
Revision 1.884 2005/07/15 15:00:01 mattias
added resourcestrings for search progress form

View File

@ -40,8 +40,8 @@ unit MakeResStrDlg;
interface
uses
Classes, SysUtils, Forms, Controls, Buttons, ComCtrls, StdCtrls, Dialogs,
LResources, LazarusIDEStrConsts, IDEOptionDefs, CodeToolManager,
Classes, SysUtils, LCLProc, Forms, Controls, Buttons, ComCtrls, StdCtrls,
Dialogs, LResources, LazarusIDEStrConsts, IDEOptionDefs, CodeToolManager,
CodeAtom, CodeToolsStructs, CodeCache, SynHighlighterPas, SynEdit,
EditorOptions, InputHistory, MiscOptions;
@ -152,6 +152,7 @@ var
Section: PCodeXYPosition;
ResourcestringSectionID: Integer;
begin
//debugln('ShowMakeResStrDialog StartPos=',dbgs(StartPos),' EndPos=',dbgs(EndPos),' ');
MakeResStrDialog:=TMakeResStrDialog.Create(nil);
MakeResStrDialog.Positions:=CodeToolBoss.Positions.CreateCopy;
MakeResStrDialog.SetSource(Code,StartPos,EndPos);
@ -601,7 +602,7 @@ var
begin
// get the Prefixes history list
HistoryList:=
InputHistories.HistoryLists.GetList(hlMakeResourceStringPrefixes,true);
InputHistories.HistoryLists.GetList(hlMakeResourceStringPrefixes,true);
IdentPrefixComboBox.Items.Assign(HistoryList);
if IdentPrefixComboBox.Items.Count>0 then
IdentPrefixComboBox.Text:=IdentPrefixComboBox.Items[0]
@ -786,9 +787,11 @@ var
LastLine: string;
NewString: String;
RightSide: String;
StartInStringConst, EndInStringConst: boolean;
begin
if not CodeToolBoss.StringConstToFormatString(Code,StartPos.X,StartPos.Y,
Code,EndPos.X,EndPos.Y,FormatStringConstant,FormatParameters)
Code,EndPos.X,EndPos.Y,FormatStringConstant,FormatParameters,
StartInStringConst,EndInStringConst)
then begin
SrcPreviewSynEdit.Text:='Error:'#13+CodeToolBoss.ErrorMessage;
exit;
@ -797,6 +800,10 @@ begin
NewString:=GetIdentifier
else
NewString:='Format('+GetIdentifier+',['+FormatParameters+'])';
if StartInStringConst then
NewString:='''+'+NewString;
if EndInStringConst then
NewString:=NewString+'+''';
LeftSide:=copy(StringConstSynEdit.Lines[0],1,StartPos.X-1);
LastLine:=StringConstSynEdit.Lines[EndPos.Y-StartPos.Y];
RightSide:=copy(LastLine,EndPos.X,length(LastLine)-EndPos.X+1);