support opening of source files

This commit is contained in:
Vadim Lopatin 2015-01-26 12:11:36 +03:00
parent 62f94db1cd
commit 8e3d0a91bc
5 changed files with 67 additions and 9 deletions

View File

@ -66,7 +66,7 @@
<debuglevel>0</debuglevel>
<debugids />
<versionlevel>0</versionlevel>
<versionids>Unicode USE_FREETYPE</versionids>
<versionids>Unicode</versionids>
<dump_source>0</dump_source>
<mapverbosity>3</mapverbosity>
<createImplib>0</createImplib>

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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;