Added syntax highlighting to search results

This commit is contained in:
Hans-Albert Maritz 2015-03-08 02:27:47 +11:00
parent 4178d63229
commit d6cffd4e2e
1 changed files with 87 additions and 30 deletions

View File

@ -12,6 +12,65 @@ import dlangide.workspace.project;
import std.string;
import std.conv;
class SearchLogWidget : LogWidget {
this(string ID){
super(ID);
}
override protected CustomCharProps[] handleCustomLineHighlight(int line, dstring txt, ref CustomCharProps[] buf) {
uint defColor = textColor;
const uint filenameColor = 0x0000C0;
const uint errorColor = 0xFF0000;
const uint warningColor = 0x606000;
const uint deprecationColor = 0x802040;
uint flags = 0;
if (buf.length < txt.length)
buf.length = txt.length;
if(txt.startsWith("Matches in ")) {
CustomCharProps[] colors = buf[0..txt.length];
uint cl = defColor;
flags = 0;
for (int i = 0; i < txt.length; i++) {
dstring rest = txt[i..$];
if(i == 11) {
cl = filenameColor;
flags = TextFlag.Underline;
}
colors[i].color = cl;
colors[i].textFlags = flags;
}
return colors;
}
else {
CustomCharProps[] colors = buf[0..txt.length];
uint cl = filenameColor;
flags = TextFlag.Underline;
for (int i = 0; i < txt.length; i++) {
dstring rest = txt[i..$];
if (rest.startsWith(" -->"d)) {
cl = warningColor;
flags = 0;
}
if(i == 4) {
cl = errorColor;
flags = TextFlag.Underline;
}
colors[i].color = cl;
colors[i].textFlags = flags;
//Colors to applay after current character.
if(rest.startsWith("]")) {
cl = defColor;
flags = 0;
}
}
return colors;
}
}
}
class SearchWidget : TabWidget {
HorizontalLayout _layout;
EditLine _findText;
@ -22,12 +81,13 @@ class SearchWidget : TabWidget {
struct SearchMatch {
int line;
long col;
string fileName;
string toString() {
return '[' ~ to!string(line) ~ ':' ~ to!string(col) ~ "] " ~ fileName;
}
dstring lineContent;
}
struct SearchMatchList {
string filename;
SearchMatch[] matches;
}
this(string ID, IDEFrame frame) {
super(ID);
@ -52,61 +112,58 @@ class SearchWidget : TabWidget {
_layout.addChild(goButton);
addChild(_layout);
_resultLog = new LogWidget("SearchLogWidget");
_resultLog = new SearchLogWidget("SearchLogWidget");
_resultLog.layoutHeight(FILL_PARENT);
addChild(_resultLog);
}
void searchInProject(ProjectItem project, ref SearchMatch[] matches, dstring text) {
void searchInProject(ProjectItem project, ref SearchMatchList[] matchList, dstring text) {
if(project.isFolder) {
foreach(ProjectItem child; cast(ProjectFolder) project) {
searchInProject(child, matches, text);
searchInProject(child, matchList, text);
}
}
else {
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) {
matches ~= SearchMatch(lineIndex+1, colIndex, project.filename);
match.matches ~= SearchMatch(lineIndex+1, colIndex, line);
}
}
}
if(match.matches.length > 0) {
matchList ~= match;
}
}
}
bool findText(dstring source) {
Log.d("Finding " ~ source);
SearchMatch[] matches;
SearchMatchList[] matches;
//TODO Should not crash when in homepage.
searchInProject(_frame.currentEditor.projectSourceFile, matches, source);
Log.d("Searching in current file " ~ source);
if(currentWorkspace) {
foreach(ProjectItem project ; currentWorkspace.projects[0].items) {
searchInProject(project, matches, source);
}
foreach(Project project; _frame._wsPanel.workspace.projects) {
searchInProject(project.items, matches, source);
}
if(matches.length == 0) {
_resultLog.appendText(to!dstring("No matches in current file." ~ '\n'));
_resultLog.appendText(to!dstring("No matches found.\n"));
}
else {
_resultLog.appendText(to!dstring("Matches found in current file: " ~ '\n'));
foreach(SearchMatch match; matches) {
if(match.fileName == _frame.currentEditor.content.filename)
_resultLog.appendText(to!dstring(" --> [" ~ to!string(match.line) ~ ':' ~ to!string(match.col) ~ "] in current file\n"));
else
_resultLog.appendText(to!dstring(" --> " ~ match.toString" ~ '\n'));
foreach(SearchMatchList fileMatchList; matches) {
_resultLog.appendText("Matches in "d ~ to!dstring(fileMatchList.filename) ~ '\n');
foreach(SearchMatch match; fileMatchList.matches) {
_resultLog.appendText(" --> ["d ~ to!dstring(match.line) ~ ":"d ~ to!dstring(match.col) ~ "]" ~ match.lineContent ~"\n"d);
}
}
}
return true;
}
}
}