From 814dcd3edeeb3ca654a59d2e1da1899a9aa386fd Mon Sep 17 00:00:00 2001
From: Vadim Lopatin <coolreader.org@gmail.com>
Date: Fri, 11 Aug 2017 19:19:30 +0300
Subject: [PATCH] .dt diet template support; trying to fix DCD issue

---
 dub.json                            |  2 +-
 src/dlangide/builders/builder.d     |  1 +
 src/dlangide/tools/d/dcdinterface.d | 34 ++++++++++++++++++++++++++---
 src/dlangide/ui/frame.d             | 11 ++++++----
 src/dlangide/ui/newfile.d           | 11 +++++++++-
 5 files changed, 50 insertions(+), 9 deletions(-)

diff --git a/dub.json b/dub.json
index 5adde40..3bd1b2a 100644
--- a/dub.json
+++ b/dub.json
@@ -12,7 +12,7 @@
     "stringImportPaths": ["views", "views/res", "views/res/i18n", "views/res/mdpi", "views/res/hdpi"],
 
     "dependencies": {
-        "dlangui": "==0.9.87",
+        "dlangui": "==0.9.88",
         "dcd": "~>0.9.0"
     },
 
diff --git a/src/dlangide/builders/builder.d b/src/dlangide/builders/builder.d
index a791109..f13213e 100644
--- a/src/dlangide/builders/builder.d
+++ b/src/dlangide/builders/builder.d
@@ -170,3 +170,4 @@ class Builder : BackgroundOperationWatcher {
             _listener(_exitCode);
     }
 }
+
diff --git a/src/dlangide/tools/d/dcdinterface.d b/src/dlangide/tools/d/dcdinterface.d
index 4bc3c65..eb6e4f8 100644
--- a/src/dlangide/tools/d/dcdinterface.d
+++ b/src/dlangide/tools/d/dcdinterface.d
@@ -12,6 +12,19 @@ import std.typecons;
 import std.conv;
 import std.string;
 
+import std.experimental.allocator;
+import std.experimental.allocator.mallocator;
+import std.experimental.allocator.gc_allocator;
+
+import server.autocomplete;
+import common.messages;
+import dsymbol.modulecache;
+
+//alias SharedASTAllocator = CAllocatorImpl!(Mallocator);
+//alias SharedASTAllocator = CAllocatorImpl!(Mallocator);
+//alias SharedASTAllocator = CSharedAllocatorImpl!(Mallocator);
+alias SharedASTAllocator = ASTAllocator;
+
 enum DCDResult : int {
     SUCCESS,
     NO_RESULT,
@@ -22,8 +35,6 @@ alias DocCommentsResultSet = Tuple!(DCDResult, "result", string[], "docComments"
 alias FindDeclarationResultSet = Tuple!(DCDResult, "result", string, "fileName", ulong, "offset");
 alias CompletionResultSet = Tuple!(DCDResult, "result", dstring[], "output", char[], "completionKinds");
 
-import server.autocomplete;
-import common.messages;
 
 class DCDTask {
     protected bool _cancelled;
@@ -85,7 +96,7 @@ class ModuleCacheAccessor {
     //protected ASTAllocator _astAllocator;
     protected ModuleCache _moduleCache;
     this(in string[] importPaths) {
-        _moduleCache = ModuleCache(new ASTAllocator);
+        _moduleCache = ModuleCache(new SharedASTAllocator);
         _moduleCache.addImportPaths(internStrings(importPaths));
     }
     protected ModuleCache * getModuleCache(in string[] importPaths) {
@@ -149,6 +160,7 @@ class DCDInterface : Thread {
     }
 
     void threadFunc() {
+        _moduleCache = new ModuleCacheAccessor(null);
         Log.d("Starting DCD tasks thread");
         while (!_queue.closed()) {
             DCDTask task;
@@ -296,3 +308,19 @@ class DCDInterface : Thread {
     }
 
 }
+
+
+/// to test broken DCD after DUB invocation
+/// run it after DCD ModuleCache is instantiated
+void testDCDFailAfterThreadCreation() {
+    import core.thread;
+
+    Log.d("testDCDFailAfterThreadCreation");
+    Thread thread = new Thread(delegate() {
+        Thread.sleep(dur!"msecs"(2000));
+    });
+    thread.start();
+    thread.join();
+    Log.d("testDCDFailAfterThreadCreation finished");
+}
+
diff --git a/src/dlangide/ui/frame.d b/src/dlangide/ui/frame.d
index 98fc4e0..6d15c85 100644
--- a/src/dlangide/ui/frame.d
+++ b/src/dlangide/ui/frame.d
@@ -46,10 +46,10 @@ import std.path;
 
 immutable string HELP_PAGE_URL = "https://github.com/buggins/dlangide/wiki";
 // TODO: get version from GIT commit
-immutable dstring DLANGIDE_VERSION = "v0.7.41"d;
+immutable dstring DLANGIDE_VERSION = "v0.7.42"d;
 
 bool isSupportedSourceTextFileFormat(string filename) {
-    return (filename.endsWith(".d") || filename.endsWith(".txt") || filename.endsWith(".cpp") || filename.endsWith(".h") || filename.endsWith(".c")
+    return (filename.endsWith(".d") || filename.endsWith(".di") || filename.endsWith(".dt") || filename.endsWith(".txt") || filename.endsWith(".cpp") || filename.endsWith(".h") || filename.endsWith(".c")
         || filename.endsWith(".json") || filename.endsWith(".sdl") || filename.endsWith(".dd") || filename.endsWith(".ddoc") || filename.endsWith(".xml") || filename.endsWith(".html")
         || filename.endsWith(".html") || filename.endsWith(".css") || filename.endsWith(".log") || filename.endsWith(".hpp"));
 }
@@ -901,8 +901,11 @@ class IDEFrame : AppFrame, ProgramExecutionStatusListener, BreakpointListChangeL
                     Platform.instance.openURL(HELP_PAGE_URL);
                     return true;
                 case IDEActions.HelpAbout:
+                    //debug {
+                    //    testDCDFailAfterThreadCreation();
+                    //}
                     window.showMessageBox(UIString.fromId("ABOUT"c) ~ " " ~ DLANGIDE_VERSION,
-                                          UIString.fromRaw("DLangIDE\n(C) Vadim Lopatin, 2014-2016\nhttp://github.com/buggins/dlangide\nIDE for D programming language written in D\nUses DlangUI library for GUI"d));
+                                          UIString.fromRaw("DLangIDE\n(C) Vadim Lopatin, 2014-2017\nhttp://github.com/buggins/dlangide\nIDE for D programming language written in D\nUses DlangUI library for GUI"d));
                     return true;
                 case StandardAction.OpenUrl:
                     platform.openURL(a.stringParam);
@@ -911,7 +914,7 @@ class IDEFrame : AppFrame, ProgramExecutionStatusListener, BreakpointListChangeL
                     UIString caption;
                     caption = UIString.fromId("HEADER_OPEN_TEXT_FILE"c);
                     FileDialog dlg = createFileDialog(caption);
-                    dlg.addFilter(FileFilterEntry(UIString.fromId("SOURCE_FILES"c), "*.d;*.dd;*.ddoc;*.di;*.dh;*.json;*.sdl;*.xml;*.ini"));
+                    dlg.addFilter(FileFilterEntry(UIString.fromId("SOURCE_FILES"c), "*.d;*.dd;*.ddoc;*.di;*.dt;*.dh;*.json;*.sdl;*.xml;*.ini"));
                     dlg.addFilter(FileFilterEntry(UIString.fromId("ALL_FILES"c), "*.*"));
                     dlg.path = _settings.getRecentPath("FILE_OPEN_PATH");
                     dlg.dialogResult = delegate(Dialog d, const Action result) {
diff --git a/src/dlangide/ui/newfile.d b/src/dlangide/ui/newfile.d
index 42f41a1..047297d 100644
--- a/src/dlangide/ui/newfile.d
+++ b/src/dlangide/ui/newfile.d
@@ -119,7 +119,7 @@ class NewFileDlg : Dialog {
         _edLocation.filetypeIcons["dub.json"] = "project-d";
         _edLocation.filetypeIcons["package.json"] = "project-d";
         _edLocation.filetypeIcons[".dlangidews"] = "project-development";
-        _edLocation.addFilter(FileFilterEntry(UIString.fromRaw("DlangIDE files"d), "*.dlangidews;*.d;*.dd;*.di;*.ddoc;*.dh;*.json;*.xml;*.ini"));
+        _edLocation.addFilter(FileFilterEntry(UIString.fromRaw("DlangIDE files"d), "*.dlangidews;*.d;*.dd;*.di;*.ddoc;*.dh;*.json;*.xml;*.ini;*.dt"));
         _edLocation.caption = "Select directory"d;
 
         // fill templates
@@ -295,6 +295,15 @@ class NewFileDlg : Dialog {
                     "\n", true);
         _templates ~= new ProjectTemplate("JSON file"d, "Empty json file."d, ".json",
                     "{\n}\n", true);
+        _templates ~= new ProjectTemplate("Vibe-D Diet Template file"d, "Empty Vibe-D Diet Template."d, ".dt",
+                                          q{
+doctype html
+html
+    head
+        title Hello, World
+    body
+        h1 Hello World
+}, true);
     }
 }