find the imports from token list, related to #77

This commit is contained in:
Basile Burg 2016-06-18 06:07:26 +02:00
parent 1fb1c8db47
commit 3818075c28
4 changed files with 68 additions and 4 deletions

View File

@ -142,6 +142,11 @@ procedure lex(const text: string; list: TLexTokenList; clbck: TLexFoundEvent = n
*)
function getModuleName(const list: TLexTokenList): string;
(**
* Fills a list with all the modules imported in a tokenized D source.
*)
procedure getImports(const list: TLexTokenList; imports: TStrings);
(**
* Compares two TPoints.
*)
@ -946,7 +951,7 @@ begin
end;
{$ENDREGION}
{$REGION Syntactic errors}
{$REGION Utils}
function TLexErrorList.getError(index: integer): TLexError;
begin
Result := PLexError(Items[index])^;
@ -1012,6 +1017,37 @@ begin
end;
end;
procedure getImports(const list: TLexTokenList; imports: TStrings);
var
i: integer;
imp: boolean;
tok: PLexToken;
itm: string = '';
begin
for i:= 0 to list.Count-1 do
begin
tok := list[i];
if (tok^.kind = ltkKeyword) and (tok^.Data = 'import') then
begin
imp := true;
continue;
end;
if not imp then
continue;
//
if (tok^.Data = '=') then
itm := ''
else if (tok^.Data = ';') or (tok^.Data = ':') or (tok^.Data = ',') then
begin
if length(itm) <> 0 then
imports.Add(itm);
itm := '';
if (tok^.Data = ';') or (tok^.Data = ':') then
imp := false;
end else
itm += tok^.Data;
end;
end;
{$ENDREGION}
end.

View File

@ -135,5 +135,9 @@ inherited CEEditorWidget: TCEEditorWidget
Caption = 'Save lexical tokens to file...'
OnClick = MenuItem5Click
end
object MenuItem8: TMenuItem
Caption = 'Save import to file...'
OnClick = MenuItem8Click
end
end
end

View File

@ -57,6 +57,7 @@ type
MenuItem3: TMenuItem;
MenuItem5: TMenuItem;
MenuItem6: TMenuItem;
MenuItem8: TMenuItem;
mnuedRename: TMenuItem;
mnuedInvAllNone: TMenuItem;
mnuedComm: TMenuItem;
@ -77,6 +78,7 @@ type
mnuEditor: TPopupMenu;
procedure MenuItem5Click(Sender: TObject);
procedure MenuItem6Click(Sender: TObject);
procedure MenuItem8Click(Sender: TObject);
procedure mnuedRenameClick(Sender: TObject);
procedure mnuedInvAllNoneClick(Sender: TObject);
procedure mnuedCommClick(Sender: TObject);
@ -802,6 +804,31 @@ begin
end;
end;
procedure TCEEditorWidget.MenuItem8Click(Sender: TObject);
var
str: TStringList;
begin
if fDoc.isNil then
exit;
if not fDoc.IsDSource and not fDoc.alwaysAdvancedFeatures then
exit;
with TSaveDialog.Create(nil) do
try
if execute then
begin
str := TStringList.Create;
fTokList.Clear;
lex(fDoc.Text, fTokList, nil, [lxoNoComments]);
getImports(fTOkList, str);
str.SaveToFile(filename);
fTokList.Clear;
str.Free;
end;
finally
free;
end;
end;
procedure TCEEditorWidget.MenuItem6Click(Sender: TObject);
begin
if fDoc.isNotNil then

View File

@ -1436,8 +1436,6 @@ procedure TCESynMemo.handleStatusChanged(Sender: TObject; Changes: TSynStatusCha
begin
if scOptions in Changes then
begin
//TODO-cLazarus: track changes in http://bugs.freepascal.org/view.php?id=30272
// and remove this workaround if they fix it.
if Beautifier.isNotNil and (Beautifier is TSynBeautifier) then
begin
if not (eoTabsToSpaces in Options) and not (eoSpacesToTabs in Options) then
@ -1549,7 +1547,6 @@ begin
loadCache;
fCacheLoaded := true;
end;
// TODO-cbugfix: follow http://bugs.freepascal.org/view.php?id=30272
if detectIndentMode then
begin
case indentationMode(lines) of