From 61e54f55f05d39f5eafc975d235aaab932f39613 Mon Sep 17 00:00:00 2001 From: Hans-Albert Maritz Date: Mon, 23 Mar 2015 10:06:01 +1100 Subject: [PATCH 1/2] Asynchronously search for text. Initial implementation. Still need to update result log. --- src/dlangide/ui/searchPanel.d | 82 +++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/src/dlangide/ui/searchPanel.d b/src/dlangide/ui/searchPanel.d index 1ebdd35..ea44e28 100644 --- a/src/dlangide/ui/searchPanel.d +++ b/src/dlangide/ui/searchPanel.d @@ -124,6 +124,18 @@ class SearchLogWidget : LogWidget { } } + +struct SearchMatch { + int line; + long col; + dstring lineContent; +} + +struct SearchMatchList { + string filename; + SearchMatch[] matches; +} + class SearchWidget : TabWidget { HorizontalLayout _layout; EditLine _findText; @@ -133,16 +145,7 @@ class SearchWidget : TabWidget { protected IDEFrame _frame; protected SearchMatchList[] _matchedList; - struct SearchMatch { - int line; - long col; - dstring lineContent; - } - - struct SearchMatchList { - string filename; - SearchMatch[] matches; - } + bool onFindButtonPressed(Widget source) { dstring txt = _findText.text; @@ -195,26 +198,18 @@ class SearchWidget : TabWidget { void searchInProject(ProjectItem project, dstring text) { if (project.isFolder == true) { ProjectFolder projFolder = cast(ProjectFolder) project; + import std.parallelism; for (int i = 0; i < projFolder.childCount; i++) { - searchInProject(projFolder.child(i), text); + taskPool.put(task(&searchInProject, projFolder.child(i), text)); } } else { Log.d("Searching in: " ~ project.filename); - EditableContent content = new EditableContent(true); - content.load(project.filename); - SearchMatchList match; - match.filename = project.filename; - - foreach(int lineIndex, dstring line; content.lines) { - auto colIndex = line.indexOf(text); - if (colIndex != -1) { - match.matches ~= SearchMatch(lineIndex, colIndex, line); - } - } - + SearchMatchList match = findMatches(project.filename, text); if(match.matches.length > 0) { - _matchedList ~= match; + synchronized { + _matchedList ~= match; + } } } } @@ -226,33 +221,39 @@ class SearchWidget : TabWidget { _resultLog.text = ""d; _matchedList = []; + import std.parallelism; + switch (_searchScope.text) { - case "File": //File + case "File": auto currentFileItem = _frame._wsPanel.workspace.findSourceFileItem(_frame.currentEditor.filename); if(currentFileItem !is null) - searchInProject(currentFileItem, source); + taskPool.put(task(&searchInProject, currentFileItem, source)); break; - case "Project": //Project + case "Project": foreach(Project project; _frame._wsPanel.workspace.projects) { if(!project.isDependency) - searchInProject(project.items, source); + taskPool.put(task(&searchInProject, project.items, source)); } break; - case "Dependencies": //Dependencies + case "Dependencies": foreach(Project project; _frame._wsPanel.workspace.projects) { if(project.isDependency) - searchInProject(project.items, source); + taskPool.put(task(&searchInProject, project.items, source)); } break; - case "Everywhere": //Everywhere + case "Everywhere": foreach(Project project; _frame._wsPanel.workspace.projects) { - searchInProject(project.items, source); + taskPool.put(task(&searchInProject, project.items, source)); } break; default: assert(0); } + + import core.thread; + Thread.sleep( dur!("msecs")(10) ); + if (_matchedList.length == 0) { _resultLog.appendText(to!dstring("No matches found.\n")); } @@ -291,3 +292,20 @@ class SearchWidget : TabWidget { return false; } } + +SearchMatchList findMatches(immutable string filename, immutable dstring searchString) { + EditableContent content = new EditableContent(true); + content.load(filename); + SearchMatchList match; + match.filename = filename; + + foreach(int lineIndex, dstring line; content.lines) { + auto colIndex = line.indexOf(searchString); + + if (colIndex != -1) { + Log.d("Match at: ", colIndex, " text: ", searchString); + match.matches ~= SearchMatch(lineIndex, colIndex, line); + } + } + return match; +} From 50a2ca18f3e02717cd610d8995c127806f761ae4 Mon Sep 17 00:00:00 2001 From: Hans-Albert Maritz Date: Mon, 23 Mar 2015 12:15:32 +1100 Subject: [PATCH 2/2] Update results when new matches are found by worker threads --- src/dlangide/ui/searchPanel.d | 47 +++++++++++++++++------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/dlangide/ui/searchPanel.d b/src/dlangide/ui/searchPanel.d index ea44e28..bed526e 100644 --- a/src/dlangide/ui/searchPanel.d +++ b/src/dlangide/ui/searchPanel.d @@ -140,11 +140,11 @@ class SearchWidget : TabWidget { HorizontalLayout _layout; EditLine _findText; SearchLogWidget _resultLog; + int _resultLogMatchIndex; ComboBox _searchScope; protected IDEFrame _frame; - protected SearchMatchList[] _matchedList; - + protected synchronized SearchMatchList[] _matchedList; bool onFindButtonPressed(Widget source) { @@ -198,9 +198,9 @@ class SearchWidget : TabWidget { void searchInProject(ProjectItem project, dstring text) { if (project.isFolder == true) { ProjectFolder projFolder = cast(ProjectFolder) project; - import std.parallelism; + import std.parallelism; for (int i = 0; i < projFolder.childCount; i++) { - taskPool.put(task(&searchInProject, projFolder.child(i), text)); + taskPool.put(task(&searchInProject, projFolder.child(i), text)); } } else { @@ -209,6 +209,7 @@ class SearchWidget : TabWidget { if(match.matches.length > 0) { synchronized { _matchedList ~= match; + invalidate(); //Widget must updated with new matches } } } @@ -220,8 +221,9 @@ class SearchWidget : TabWidget { _resultLog.textToHighlight = ""d; _resultLog.text = ""d; _matchedList = []; + _resultLogMatchIndex = 0; - import std.parallelism; + import std.parallelism; //for taskpool. switch (_searchScope.text) { case "File": @@ -249,26 +251,24 @@ class SearchWidget : TabWidget { default: assert(0); } - - - import core.thread; - Thread.sleep( dur!("msecs")(10) ); - - if (_matchedList.length == 0) { - _resultLog.appendText(to!dstring("No matches found.\n")); - } - else { - _resultLog.textToHighlight = source; - foreach(SearchMatchList fileMatchList; _matchedList) { - _resultLog.appendText("Matches in "d ~ to!dstring(fileMatchList.filename) ~ '\n'); - foreach(SearchMatch match; fileMatchList.matches) { - _resultLog.appendText(" --> ["d ~ to!dstring(match.line+1) ~ ":"d ~ to!dstring(match.col) ~ "]" ~ match.lineContent ~"\n"d); - } - } - } + _resultLog.textToHighlight = source; return true; } + override void onDraw(DrawBuf buf) { + //Check if there are new matches to display + if(_resultLogMatchIndex < _matchedList.length) { + for(; _resultLogMatchIndex < _matchedList.length; _resultLogMatchIndex++) { + SearchMatchList matchList = _matchedList[_resultLogMatchIndex]; + _resultLog.appendText("Matches in "d ~ to!dstring(matchList.filename) ~ '\n'); + foreach(SearchMatch match; matchList.matches) { + _resultLog.appendText(" --> ["d ~ to!dstring(match.line+1) ~ ":"d ~ to!dstring(match.col) ~ "]" ~ match.lineContent ~"\n"d); + } + } + } + super.onDraw(buf); + } + //Find the match/matchList that corrosponds to the line in _resultLog bool onMatchClick(int line) { line++; @@ -293,7 +293,7 @@ class SearchWidget : TabWidget { } } -SearchMatchList findMatches(immutable string filename, immutable dstring searchString) { +SearchMatchList findMatches(in string filename, in dstring searchString) { EditableContent content = new EditableContent(true); content.load(filename); SearchMatchList match; @@ -303,7 +303,6 @@ SearchMatchList findMatches(immutable string filename, immutable dstring searchS auto colIndex = line.indexOf(searchString); if (colIndex != -1) { - Log.d("Match at: ", colIndex, " text: ", searchString); match.matches ~= SearchMatch(lineIndex, colIndex, line); } }