Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a directory and package.d #374

Merged
merged 16 commits into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adding a default package.d now works
  • Loading branch information
Zevenberge committed Dec 23, 2017
commit 87d5a88a0c403cede059caaa0917d293b017c8b2
132 changes: 75 additions & 57 deletions src/dlangide/ui/newfile.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Loading