diff --git a/dlangide.visualdproj b/dlangide.visualdproj index aff27ae..ae3a58b 100644 --- a/dlangide.visualdproj +++ b/dlangide.visualdproj @@ -66,7 +66,7 @@ 0 0 - Unicode USE_FREETYPE + Unicode 0 3 0 diff --git a/src/dlangide/ui/frame.d b/src/dlangide/ui/frame.d index cf3e11f..d1034dc 100644 --- a/src/dlangide/ui/frame.d +++ b/src/dlangide/ui/frame.d @@ -12,6 +12,7 @@ import dlangui.widgets.toolbars; import dlangui.widgets.combobox; import dlangui.dialogs.dialog; import dlangui.dialogs.filedlg; +import dlangui.core.stdaction; import dlangide.ui.commands; import dlangide.ui.wspanel; @@ -23,6 +24,13 @@ import dlangide.workspace.project; import std.conv; import std.utf; import std.algorithm; +import std.path; + +bool isSupportedSourceTextFileFormat(string filename) { + return (filename.endsWith(".d") || filename.endsWith(".txt") || filename.endsWith(".cpp") || filename.endsWith(".h") || filename.endsWith(".c") + || filename.endsWith(".json") || filename.endsWith(".dd") || filename.endsWith(".ddoc") || filename.endsWith(".xml") || filename.endsWith(".html") + || filename.endsWith(".html") || filename.endsWith(".css") || filename.endsWith(".log") || filename.endsWith(".hpp")); +} /// DIDE app frame class IDEFrame : AppFrame { @@ -54,17 +62,24 @@ class IDEFrame : AppFrame { /// source file selected in workspace tree bool onSourceFileSelected(ProjectSourceFile file, bool activate) { Log.d("onSourceFileSelected ", file.filename); - int index = _tabs.tabIndex(file.filename); + return openSourceFile(file.filename, file, activate); + } + + bool openSourceFile(string filename, ProjectSourceFile file = null, bool activate = true) { + if (!file) + file = _wsPanel.findSourceFileItem(filename); + Log.d("openSourceFile ", filename); + int index = _tabs.tabIndex(filename); if (index >= 0) { // file is already opened in tab _tabs.selectTab(index, true); } else { // open new file - DSourceEdit editor = new DSourceEdit(file.filename); - if (editor.load(file)) { - _tabs.addTab(editor, toUTF32(file.name)); - index = _tabs.tabIndex(file.filename); - TabItem tab = _tabs.tab(file.filename); + DSourceEdit editor = new DSourceEdit(filename); + if (file ? editor.load(file) : editor.load(filename)) { + _tabs.addTab(editor, toUTF32(baseName(filename))); + index = _tabs.tabIndex(filename); + TabItem tab = _tabs.tab(filename); tab.objectParam = file; _tabs.selectTab(index, true); } else { @@ -75,7 +90,7 @@ class IDEFrame : AppFrame { } } if (activate) { - focusEditor(file.filename); + focusEditor(filename); } requestLayout(); return true; @@ -185,7 +200,12 @@ class IDEFrame : AppFrame { caption = "Open Text File"d; FileDialog dlg = new FileDialog(caption, window, null); dlg.onDialogResult = delegate(Dialog dlg, const Action result) { - // + if (result.id == ACTION_OPEN.id) { + string filename = result.stringParam; + if (isSupportedSourceTextFileFormat(filename)) { + openSourceFile(filename); + } + } }; dlg.show(); return true; diff --git a/src/dlangide/ui/wspanel.d b/src/dlangide/ui/wspanel.d index 30d4fb3..0dbc6ed 100644 --- a/src/dlangide/ui/wspanel.d +++ b/src/dlangide/ui/wspanel.d @@ -65,6 +65,12 @@ class WorkspacePanel : DockWindow { return _workspace; } + ProjectSourceFile findSourceFileItem(string filename) { + if (_workspace) + return _workspace.findSourceFileItem(filename); + return null; + } + void addProjectItems(TreeItem root, ProjectItem items) { for (int i = 0; i < items.childCount; i++) { ProjectItem child = items.child(i); diff --git a/src/dlangide/workspace/project.d b/src/dlangide/workspace/project.d index 14f928d..c170d59 100644 --- a/src/dlangide/workspace/project.d +++ b/src/dlangide/workspace/project.d @@ -7,6 +7,7 @@ import std.path; import std.file; import std.json; import std.utf; +import std.algorithm; /// project item class ProjectItem { @@ -218,6 +219,27 @@ class Project : WorkspaceItem { return folder; } + /// tries to find source file in project, returns found project source file item, or null if not found + ProjectSourceFile findSourceFileItem(ProjectItem dir, string filename) { + for (int i = 0; i < dir.childCount; i++) { + ProjectItem item = dir.child(i); + if (item.isFolder) { + ProjectSourceFile res = findSourceFileItem(item, filename); + if (res) + return res; + } else { + ProjectSourceFile res = cast(ProjectSourceFile)item; + if (res && res.filename.equal(filename)) + return res; + } + } + return null; + } + + ProjectSourceFile findSourceFileItem(string filename) { + return findSourceFileItem(_items, filename); + } + override bool load(string fname = null) { if (fname.length > 0) filename = fname; diff --git a/src/dlangide/workspace/workspace.d b/src/dlangide/workspace/workspace.d index 75afed2..003ad7e 100644 --- a/src/dlangide/workspace/workspace.d +++ b/src/dlangide/workspace/workspace.d @@ -31,6 +31,16 @@ class Workspace : WorkspaceItem { return _projects; } + /// tries to find source file in one of projects, returns found project source file item, or null if not found + ProjectSourceFile findSourceFileItem(string filename) { + foreach (Project p; _projects) { + ProjectSourceFile res = p.findSourceFileItem(filename); + if (res) + return res; + } + return null; + } + override bool load(string fname = null) { if (fname.length > 0) filename = fname;