fix #251 - new package creation

This commit is contained in:
Vadim Lopatin 2017-09-06 18:09:13 +03:00
parent 8e118cd5ae
commit 1f34e7dc6c
1 changed files with 34 additions and 10 deletions

View File

@ -216,7 +216,7 @@ class NewFileDlg : Dialog {
if (!exists(_location) || !isDir(_location)) if (!exists(_location) || !isDir(_location))
return setError("Location directory does not exist"); return setError("Location directory does not exist");
if (_currentTemplate.isModule) { if (_currentTemplate.kind == FileKind.MODULE || _currentTemplate.kind == FileKind.PACKAGE) {
string sourcePath, relativePath; string sourcePath, relativePath;
if (!findSource(_location, sourcePath, relativePath)) if (!findSource(_location, sourcePath, relativePath))
return setError("Location is outside of source path"); return setError("Location is outside of source path");
@ -231,7 +231,12 @@ class NewFileDlg : Dialog {
buf ~= ch; buf ~= ch;
} }
_packageName = buf.dup; _packageName = buf.dup;
string m = !_packageName.empty ? _packageName ~ '.' ~ _moduleName : _moduleName; string m;
if (_currentTemplate.kind == FileKind.MODULE) {
m = !_packageName.empty ? _packageName ~ '.' ~ _moduleName : _moduleName;
} else {
m = _packageName;
}
_edModuleName.text = toUTF32(m); _edModuleName.text = toUTF32(m);
_packageName = m; _packageName = m;
} else { } else {
@ -248,9 +253,12 @@ class NewFileDlg : Dialog {
private FileCreationResult _result; private FileCreationResult _result;
bool createItem() { bool createItem() {
try { try {
if (_currentTemplate.isModule) { if (_currentTemplate.kind == FileKind.MODULE) {
string txt = "module " ~ _packageName ~ ";\n\n" ~ _currentTemplate.srccode; string txt = "module " ~ _packageName ~ ";\n\n" ~ _currentTemplate.srccode;
write(_fullPathName, txt); write(_fullPathName, txt);
} else if (_currentTemplate.kind == FileKind.PACKAGE) {
string txt = "package " ~ _packageName ~ ";\n\n" ~ _currentTemplate.srccode;
write(_fullPathName, txt);
} else { } else {
write(_fullPathName, _currentTemplate.srccode); write(_fullPathName, _currentTemplate.srccode);
} }
@ -284,17 +292,27 @@ class NewFileDlg : Dialog {
_currentTemplateIndex = index; _currentTemplateIndex = index;
_currentTemplate = _templates[index]; _currentTemplate = _templates[index];
_templateDescription.text = _currentTemplate.description; _templateDescription.text = _currentTemplate.description;
if (_currentTemplate.kind == FileKind.PACKAGE) {
_edFileName.enabled = false;
_edFileName.text = "package"d;
} else {
if (_edFileName.text == "package")
_edFileName.text = "newfile";
_edFileName.enabled = true;
}
//updateDirLayout(); //updateDirLayout();
validate(); validate();
} }
void initTemplates() { void initTemplates() {
_templates ~= new ProjectTemplate("Empty module"d, "Empty D module file."d, ".d", _templates ~= new ProjectTemplate("Empty module"d, "Empty D module file."d, ".d",
"\n", true); "\n", FileKind.MODULE);
_templates ~= new ProjectTemplate("Package"d, "D package."d, ".d",
"\n", FileKind.PACKAGE);
_templates ~= new ProjectTemplate("Text file"d, "Empty text file."d, ".txt", _templates ~= new ProjectTemplate("Text file"d, "Empty text file."d, ".txt",
"\n", true); "\n", FileKind.TEXT);
_templates ~= new ProjectTemplate("JSON file"d, "Empty json file."d, ".json", _templates ~= new ProjectTemplate("JSON file"d, "Empty json file."d, ".json",
"{\n}\n", true); "{\n}\n", FileKind.TEXT);
_templates ~= new ProjectTemplate("Vibe-D Diet Template file"d, "Empty Vibe-D Diet Template."d, ".dt", _templates ~= new ProjectTemplate("Vibe-D Diet Template file"d, "Empty Vibe-D Diet Template."d, ".dt",
q{ q{
doctype html doctype html
@ -303,22 +321,28 @@ html
title Hello, World title Hello, World
body body
h1 Hello World h1 Hello World
}, true); }, FileKind.TEXT);
} }
} }
enum FileKind {
MODULE,
PACKAGE,
TEXT,
}
class ProjectTemplate { class ProjectTemplate {
dstring name; dstring name;
dstring description; dstring description;
string fileExtension; string fileExtension;
string srccode; string srccode;
bool isModule; FileKind kind;
this(dstring name, dstring description, string fileExtension, string srccode, bool isModule) { this(dstring name, dstring description, string fileExtension, string srccode, FileKind kind) {
this.name = name; this.name = name;
this.description = description; this.description = description;
this.fileExtension = fileExtension; this.fileExtension = fileExtension;
this.srccode = srccode; this.srccode = srccode;
this.isModule = isModule; this.kind = kind;
} }
} }