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; 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. * Compares two TPoints.
*) *)
@ -946,7 +951,7 @@ begin
end; end;
{$ENDREGION} {$ENDREGION}
{$REGION Syntactic errors} {$REGION Utils}
function TLexErrorList.getError(index: integer): TLexError; function TLexErrorList.getError(index: integer): TLexError;
begin begin
Result := PLexError(Items[index])^; Result := PLexError(Items[index])^;
@ -1012,6 +1017,37 @@ begin
end; end;
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} {$ENDREGION}
end. end.

View File

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

View File

@ -57,6 +57,7 @@ type
MenuItem3: TMenuItem; MenuItem3: TMenuItem;
MenuItem5: TMenuItem; MenuItem5: TMenuItem;
MenuItem6: TMenuItem; MenuItem6: TMenuItem;
MenuItem8: TMenuItem;
mnuedRename: TMenuItem; mnuedRename: TMenuItem;
mnuedInvAllNone: TMenuItem; mnuedInvAllNone: TMenuItem;
mnuedComm: TMenuItem; mnuedComm: TMenuItem;
@ -77,6 +78,7 @@ type
mnuEditor: TPopupMenu; mnuEditor: TPopupMenu;
procedure MenuItem5Click(Sender: TObject); procedure MenuItem5Click(Sender: TObject);
procedure MenuItem6Click(Sender: TObject); procedure MenuItem6Click(Sender: TObject);
procedure MenuItem8Click(Sender: TObject);
procedure mnuedRenameClick(Sender: TObject); procedure mnuedRenameClick(Sender: TObject);
procedure mnuedInvAllNoneClick(Sender: TObject); procedure mnuedInvAllNoneClick(Sender: TObject);
procedure mnuedCommClick(Sender: TObject); procedure mnuedCommClick(Sender: TObject);
@ -802,6 +804,31 @@ begin
end; end;
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); procedure TCEEditorWidget.MenuItem6Click(Sender: TObject);
begin begin
if fDoc.isNotNil then if fDoc.isNotNil then

View File

@ -1436,8 +1436,6 @@ procedure TCESynMemo.handleStatusChanged(Sender: TObject; Changes: TSynStatusCha
begin begin
if scOptions in Changes then if scOptions in Changes then
begin 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 if Beautifier.isNotNil and (Beautifier is TSynBeautifier) then
begin begin
if not (eoTabsToSpaces in Options) and not (eoSpacesToTabs in Options) then if not (eoTabsToSpaces in Options) and not (eoSpacesToTabs in Options) then
@ -1549,7 +1547,6 @@ begin
loadCache; loadCache;
fCacheLoaded := true; fCacheLoaded := true;
end; end;
// TODO-cbugfix: follow http://bugs.freepascal.org/view.php?id=30272
if detectIndentMode then if detectIndentMode then
begin begin
case indentationMode(lines) of case indentationMode(lines) of