From 87d5a88a0c403cede059caaa0917d293b017c8b2 Mon Sep 17 00:00:00 2001 From: Zevenberge Date: Sat, 23 Dec 2017 18:09:04 +0100 Subject: [PATCH] Adding a default package.d now works --- src/dlangide/ui/newfile.d | 132 +++++++++-------- src/dlangide/ui/newfolder.d | 272 ++++++++++++++++++++---------------- 2 files changed, 223 insertions(+), 181 deletions(-) diff --git a/src/dlangide/ui/newfile.d b/src/dlangide/ui/newfile.d index 9407008..049b476 100644 --- a/src/dlangide/ui/newfile.d +++ b/src/dlangide/ui/newfile.d @@ -198,29 +198,6 @@ class NewFileDlg : Dialog { ProjectTemplate _currentTemplate; ProjectTemplate[] _templates; - static bool isSubdirOf(string path, string basePath) { - if (path.equal(basePath)) - return true; - if (path.length > basePath.length + 1 && path.startsWith(basePath)) { - char ch = path[basePath.length]; - return ch == '/' || ch == '\\'; - } - return false; - } - - bool findSource(string path, ref string sourceFolderPath, ref string relativePath) { - foreach(dir; _sourcePaths) { - if (isSubdirOf(path, dir)) { - sourceFolderPath = dir; - relativePath = path[sourceFolderPath.length .. $]; - if (relativePath.length > 0 && (relativePath[0] == '\\' || relativePath[0] == '/')) - relativePath = relativePath[1 .. $]; - return true; - } - } - return false; - } - bool setError(dstring msg) { _statusText.text = msg; return msg.empty; @@ -241,26 +218,13 @@ class NewFileDlg : Dialog { return setError("Location directory does not exist"); if (_currentTemplate.kind == FileKind.MODULE || _currentTemplate.kind == FileKind.PACKAGE) { - string sourcePath, relativePath; - if (!findSource(_location, sourcePath, relativePath)) - return setError("Location is outside of source path"); - if (!isValidModuleName(filename)) - return setError("Invalid file name"); - _moduleName = filename; - char[] buf; - foreach(c; relativePath) { - char ch = c; - if (ch == '/' || ch == '\\') - ch = '.'; - else if (ch == '.') - ch = '_'; - if (ch == '.' && (buf.length == 0 || buf[$-1] == '.')) - continue; // skip duplicate . - buf ~= ch; - } - if (buf.length && buf[$-1] == '.') - buf.length--; - _packageName = buf.dup; + string sourcePath, relativePath; + if (!findSource(_sourcePaths, _location, sourcePath, relativePath)) + return setError("Location is outside of source path"); + if (!isValidModuleName(filename)) + return setError("Invalid file name"); + _moduleName = filename; + _packageName = getPackageName(sourcePath, relativePath); string m; if (_currentTemplate.kind == FileKind.MODULE) { m = !_packageName.empty ? _packageName ~ '.' ~ _moduleName : _moduleName; @@ -284,20 +248,10 @@ class NewFileDlg : Dialog { private FileCreationResult _result; bool createItem() { - try { - if (_currentTemplate.kind == FileKind.MODULE) { - string txt = "module " ~ _packageName ~ ";\n\n" ~ _currentTemplate.srccode; - write(_fullPathName, txt); - } else if (_currentTemplate.kind == FileKind.PACKAGE) { - string txt = "module " ~ _packageName ~ ";\n\n" ~ _currentTemplate.srccode; - write(_fullPathName, txt); - } else { - write(_fullPathName, _currentTemplate.srccode); - } - } catch (Exception e) { - Log.e("Cannot create file", e); - return setError("Cannot create file"); - } + if(!createFile(_fullPathName, _currentTemplate.kind, _packageName, _currentTemplate.srccode)) { + return setError("Cannot create file"); + } + _result = new FileCreationResult(_project, _fullPathName); return true; } @@ -377,3 +331,67 @@ class ProjectTemplate { this.kind = kind; } } + +bool createFile(string fullPathName, FileKind fileKind, string packageName, string sourceCode) { + try { + if (fileKind == FileKind.MODULE) { + string txt = "module " ~ packageName ~ ";\n\n" ~ sourceCode; + write(fullPathName, txt); + } else if (fileKind == FileKind.PACKAGE) { + string txt = "module " ~ packageName ~ ";\n\n" ~ sourceCode; + write(fullPathName, txt); + } else { + write(fullPathName, sourceCode); + } + return true; + } + catch(Exception e) { + Log.e("Cannot create file", e); + return false; + } +} + +string getPackageName(string path, string[] sourcePaths){ + string sourcePath, relativePath; + if(!findSource(sourcePaths, path, sourcePath, relativePath)) return ""; + return getPackageName(sourcePath, relativePath); +} + +string getPackageName(string sourcePath, string relativePath){ + + char[] buf; + foreach(c; relativePath) { + char ch = c; + if (ch == '/' || ch == '\\') + ch = '.'; + else if (ch == '.') + ch = '_'; + if (ch == '.' && (buf.length == 0 || buf[$-1] == '.')) + continue; // skip duplicate . + buf ~= ch; + } + if (buf.length && buf[$-1] == '.') + buf.length--; + return buf.dup; +} +private bool findSource(string[] sourcePaths, string path, ref string sourceFolderPath, ref string relativePath) { + foreach(dir; sourcePaths) { + if (isSubdirOf(path, dir)) { + sourceFolderPath = dir; + relativePath = path[sourceFolderPath.length .. $]; + if (relativePath.length > 0 && (relativePath[0] == '\\' || relativePath[0] == '/')) + relativePath = relativePath[1 .. $]; + return true; + } + } + return false; +} +private bool isSubdirOf(string path, string basePath) { + if (path.equal(basePath)) + return true; + if (path.length > basePath.length + 1 && path.startsWith(basePath)) { + char ch = path[basePath.length]; + return ch == '/' || ch == '\\'; + } + return false; +} \ No newline at end of file diff --git a/src/dlangide/ui/newfolder.d b/src/dlangide/ui/newfolder.d index 1cfd53b..d5bb665 100644 --- a/src/dlangide/ui/newfolder.d +++ b/src/dlangide/ui/newfolder.d @@ -2,7 +2,7 @@ import std.array : empty; import std.file : mkdir, exists; -import std.path : buildNormalizedPath; +import std.path : buildPath, buildNormalizedPath; import std.utf : toUTF32; import dlangui.core.logger; @@ -19,144 +19,168 @@ import dlangide.ui.newfile; import dlangide.workspace.project; class NewFolderDialog : Dialog { - private { - IDEFrame _ide; - Project _project; - ProjectFolder _folder; - string _location; - } + private { + IDEFrame _ide; + Project _project; + ProjectFolder _folder; + string _location; + } - - this(IDEFrame parent, Project currentProject, ProjectFolder folder) { - super(UIString.fromId("OPTION_NEW_SOURCE_FILE"c), parent.window, - DialogFlag.Modal | DialogFlag.Resizable | DialogFlag.Popup, 800, 0); + + this(IDEFrame parent, Project currentProject, ProjectFolder folder) { + super(UIString.fromId("OPTION_NEW_SOURCE_FILE"c), parent.window, + DialogFlag.Modal | DialogFlag.Resizable | DialogFlag.Popup, 800, 0); layoutWidth = FILL_PARENT; - _ide = parent; - _icon = "dlangui-logo1"; - this._project = currentProject; - this._folder = folder; - if (folder){ - _location = folder.filename; - } - else { - _location = currentProject.dir; - } - } + _ide = parent; + _icon = "dlangui-logo1"; + this._project = currentProject; + this._folder = folder; + if (folder){ + _location = folder.filename; + } + else { + _location = currentProject.dir; + } + } - override void initialize() { - super.initialize(); - Widget content; - try { - content = parseML(q{ - VerticalLayout { - id: vlayout - padding: Rect { 5, 5, 5, 5 } - layoutWidth: fill; layoutHeight: wrap - TableLayout { - margins: 5 - colCount: 2 - layoutWidth: fill; layoutHeight: wrap - TextWidget { text: NAME } - EditLine { id: edName; text: "newfolder"; layoutWidth: fill } - TextWidget { text: OPTION_FILE_PATH } - EditLine { id: edFilePath; text: ""; layoutWidth: wrap; readOnly: true } - } - TextWidget { id: statusText; text: ""; layoutWidth: fill; textColor: #FF0000 } - } - }); - } catch (Exception e) { - Log.e("Exceptin while parsing DML", e); - throw e; - } - _edFileName = content.childById!EditLine("edName"); - _edFilePath = content.childById!EditLine("edFilePath"); - _statusText = content.childById!TextWidget("statusText"); + override void initialize() { + super.initialize(); + Widget content; + try { + content = parseML(q{ + VerticalLayout { + id: vlayout + padding: Rect { 5, 5, 5, 5 } + layoutWidth: fill; layoutHeight: wrap + TableLayout { + margins: 5 + colCount: 2 + layoutWidth: fill; layoutHeight: wrap + TextWidget { text: NAME } + EditLine { id: fileName; text: "newfolder"; layoutWidth: fill } + CheckBox { id: makePackage } + TextWidget { text: MAKE_PACKAGE} + } + TextWidget { id: statusText; text: ""; layoutWidth: fill; textColor: #FF0000 } + } + }); + } catch (Exception e) { + Log.e("Exceptin while parsing DML", e); + throw e; + } + _edFileName = content.childById!EditLine("fileName"); + _edMakePackage = content.childById!CheckBox("makePackage"); + _statusText = content.childById!TextWidget("statusText"); - _edFileName.enterKey.connect(&onEnterKey); - _edFilePath.enterKey.connect(&onEnterKey); + _edFileName.enterKey.connect(&onEnterKey); - _edFileName.setDefaultPopupMenu(); - _edFilePath.setDefaultPopupMenu(); + _edFileName.setDefaultPopupMenu(); - _edFileName.contentChange = delegate (EditableContent source) { - updateValues(source.text); - validate(); - }; + _edFileName.contentChange = delegate (EditableContent source) { + updateValues(source.text); + validate(); + }; - addChild(content); - addChild(createButtonsPanel([ACTION_FILE_NEW_DIRECTORY, ACTION_CANCEL], 0, 0)); + addChild(content); + addChild(createButtonsPanel([ACTION_FILE_NEW_DIRECTORY, ACTION_CANCEL], 0, 0)); - updateValues(_edFileName.text); - } + updateValues(_edFileName.text); + } - override void onShow() { - super.onShow(); - _edFileName.selectAll(); - _edFileName.setFocus(); - } + override void onShow() { + super.onShow(); + _edFileName.selectAll(); + _edFileName.setFocus(); + } - protected bool onEnterKey(EditWidgetBase editor) { - if (!validate()) - return false; - close(_buttonActions[0]); - return true; - } + protected bool onEnterKey(EditWidgetBase editor) { + if (!validate()) + return false; + close(_buttonActions[0]); + return true; + } - private bool validate() { - if (!isValidModuleName(_fileName)) - return setError("Invalid folder name"); - return setError(null); - } + private bool validate() { + if (!isValidModuleName(_fileName)) + return setError("Invalid folder name"); + return setError(null); + } - private void updateValues(dstring fileName) { - _fileName = toUTF8(fileName); - _edFilePath.text = toUTF32(fullPathName); - } + private void updateValues(dstring fileName) { + _fileName = toUTF8(fileName); + } - private bool setError(dstring msg) { - _statusText.text = msg; - return msg.empty; - } + private bool setError(dstring msg) { + _statusText.text = msg; + return msg.empty; + } - private { - EditLine _edFileName; - EditLine _edFilePath; - TextWidget _statusText; + private { + EditLine _edFileName; + CheckBox _edMakePackage; + TextWidget _statusText; - string _fileName = "newfile"; - FileCreationResult _result; - string fullPathName() @property { - return buildNormalizedPath(_location, _fileName); - } - } + string _fileName = "newfile"; + FileCreationResult _result; + bool shouldMakePackage() @property { + return _edMakePackage.checked; + } + string fullPathName() @property { + return buildNormalizedPath(_location, _fileName); + } + } - private bool createItem() { - string fullPathName = this.fullPathName; - if(exists(fullPathName)) - return setError("Folder already exists"); - try { - mkdir(fullPathName); - } catch (Exception e) { - Log.e("Cannot create folder", e); - return setError("Cannot create folder"); - } - _result = new FileCreationResult(_project, fullPathName); - return true; - } + private bool createItem() { + string fullPathName = this.fullPathName; + if(exists(fullPathName)) + return setError("Folder already exists"); - override void close(const Action action) { - Action newaction = action.clone(); - if (action.id == IDEActions.FileNewDirectory) { - if (!validate()) { - window.showMessageBox(UIString.fromId("ERROR"c), UIString.fromId("ERROR_INVALID_PARAMETERS"c)); - return; - } - if (!createItem()) { - window.showMessageBox(UIString.fromId("ERROR"c), UIString.fromId("ERROR_INVALID_PARAMETERS"c)); - return; - } - newaction.objectParam = _result; - } - super.close(newaction); - } + if(!makeDirectory(fullPathName)) return false; + if(shouldMakePackage) { + if(!makePackageFile(fullPathName)) { + return false; + } + } + _result = new FileCreationResult(_project, fullPathName); + return true; + } + + private bool makeDirectory(string fullPathName) { + try { + mkdir(fullPathName); + return true; + } catch (Exception e) { + Log.e("Cannot create folder", e); + return setError("Cannot create folder"); + } + } + + private bool makePackageFile(string fullPathName) { + string packageName = getPackageName(fullPathName, _project.sourcePaths); + if(packageName.empty) { + Log.e("Could not determing package name for ", fullPathName); + return false; + } + if(!createFile(fullPathName.buildPath("package.d"), FileKind.PACKAGE, packageName, null)) { + Log.e("Could not create package file in folder ", fullPathName); + return false; + } + return true; + } + + override void close(const Action action) { + Action newaction = action.clone(); + if (action.id == IDEActions.FileNewDirectory) { + if (!validate()) { + window.showMessageBox(UIString.fromId("ERROR"c), UIString.fromId("ERROR_INVALID_PARAMETERS"c)); + return; + } + if (!createItem()) { + window.showMessageBox(UIString.fromId("ERROR"c), UIString.fromId("ERROR_INVALID_PARAMETERS"c)); + return; + } + newaction.objectParam = _result; + } + super.close(newaction); + } } \ No newline at end of file