From 436778fd1b30b061d812ac6162dc513b57d6b2dd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laurent=20Tr=C3=A9guier?= <laurent@treguier.org>
Date: Fri, 10 Aug 2018 09:15:07 +0200
Subject: [PATCH] fix #521 - Add a request allowing to remove a set of import
 path (#522)

fix #521 - Add a request allowing to remove a set of import path
merged-on-behalf-of: BBasile <BBasile@users.noreply.github.com>
---
 README.md                        |  6 ++++++
 dsymbol                          |  2 +-
 dub.json                         |  2 +-
 src/dcd/client/client.d          | 22 +++++++++++++---------
 src/dcd/common/messages.d        |  4 +++-
 src/dcd/server/main.d            |  5 +++++
 tests/run_tests.sh               |  1 +
 tests/tc_rm_import/expected1.txt |  2 ++
 tests/tc_rm_import/expected2.txt |  0
 tests/tc_rm_import/file.d        |  1 +
 tests/tc_rm_import/run.sh        | 11 +++++++++++
 11 files changed, 44 insertions(+), 12 deletions(-)
 create mode 100644 tests/tc_rm_import/expected1.txt
 create mode 100644 tests/tc_rm_import/expected2.txt
 create mode 100644 tests/tc_rm_import/file.d
 create mode 100755 tests/tc_rm_import/run.sh

diff --git a/README.md b/README.md
index 4f10cb9..c2adb4b 100644
--- a/README.md
+++ b/README.md
@@ -224,6 +224,12 @@ this, run the client with the -I option:
 
 	dcd-client -Ipath/to/imports
 
+## Remove import search path
+
+Import paths can be removed from the server without restarting it. To accomplish
+this, run the client with the -R option:
+
+	dcd-client -Rpath/to/imports
 
 ## Find declaration of symbol at cursor
 
diff --git a/dsymbol b/dsymbol
index 3b546ed..3a562af 160000
--- a/dsymbol
+++ b/dsymbol
@@ -1 +1 @@
-Subproject commit 3b546ed2b2551f61e0cf30d04f45682546387422
+Subproject commit 3a562af26b008084c7f70520f5b91b6357a7c919
diff --git a/dub.json b/dub.json
index 124bf13..008456b 100644
--- a/dub.json
+++ b/dub.json
@@ -7,7 +7,7 @@
   ],
   "license": "GPL-3.0",
   "dependencies": {
-    "dsymbol": "~>0.4.1",
+    "dsymbol": "~>0.4.3",
     "libdparse": "~>0.9.1",
     "msgpack-d": "~>1.0.0-beta.3",
     "stdx-allocator": "~>2.77.2"
diff --git a/src/dcd/client/client.d b/src/dcd/client/client.d
index a88587d..18a4207 100644
--- a/src/dcd/client/client.d
+++ b/src/dcd/client/client.d
@@ -39,7 +39,8 @@ int main(string[] args)
 	sharedLog.fatalHandler = () {};
 
 	size_t cursorPos = size_t.max;
-	string[] importPaths;
+	string[] addedImportPaths;
+	string[] removedImportPaths;
 	ushort port;
 	bool help;
 	bool shutdown;
@@ -66,10 +67,11 @@ int main(string[] args)
 
 	try
 	{
-		getopt(args, "cursorPos|c", &cursorPos, "I", &importPaths,
-			"port|p", &port, "help|h", &help, "shutdown", &shutdown,
-			"clearCache", &clearCache, "symbolLocation|l", &symbolLocation,
-			"doc|d", &doc, "query|status|q", &query, "search|s", &search,
+		getopt(args, "cursorPos|c", &cursorPos, "I", &addedImportPaths,
+			"R", &removedImportPaths, "port|p", &port, "help|h", &help,
+			"shutdown", &shutdown, "clearCache", &clearCache,
+			"symbolLocation|l", &symbolLocation, "doc|d", &doc,
+			"query|status|q", &query, "search|s", &search,
 			"version", &printVersion, "listImports", &listImports,
 			"tcp", &useTCP, "socketFile", &socketFile,
 			"getIdentifier", &getIdentifier,
@@ -136,10 +138,12 @@ int main(string[] args)
 		scope (exit) { socket.shutdown(SocketShutdown.BOTH); socket.close(); }
 		return sendRequest(socket, request) ? 0 : 1;
 	}
-	else if (importPaths.length > 0)
+	else if (addedImportPaths.length > 0 || removedImportPaths.length > 0)
 	{
-		request.kind |= RequestKind.addImport;
-		request.importPaths = importPaths.map!(a => absolutePath(a)).array;
+		immutable bool adding = addedImportPaths.length > 0;
+		request.kind |= adding ? RequestKind.addImport : RequestKind.removeImport;
+		request.importPaths = (adding ? addedImportPaths : removedImportPaths)
+			.map!(a => absolutePath(a)).array;
 		if (cursorPos == size_t.max)
 		{
 			Socket socket = createSocket(socketFile, port);
@@ -199,7 +203,7 @@ int main(string[] args)
 	}
 
 	request.fileName = fileName;
-	request.importPaths = importPaths;
+	request.importPaths = addedImportPaths;
 	request.sourceCode = sourceCode;
 	request.cursorPosition = cursorPos;
 	request.searchName = search;
diff --git a/src/dcd/common/messages.d b/src/dcd/common/messages.d
index 3eeeb7f..8453d7b 100644
--- a/src/dcd/common/messages.d
+++ b/src/dcd/common/messages.d
@@ -74,8 +74,10 @@ enum RequestKind : ushort
 	search =         0b00000000_10000000,
 	/// List import directories
 	listImports =    0b00000001_00000000,
-	/// local symbol usage
+	/// Local symbol usage
 	localUse =     	 0b00000010_00000000,
+	/// Remove import directory from server
+	removeImport =   0b00000100_00000000,
 	// dfmt on
 }
 
diff --git a/src/dcd/server/main.d b/src/dcd/server/main.d
index 9e19c3e..ee586a1 100644
--- a/src/dcd/server/main.d
+++ b/src/dcd/server/main.d
@@ -256,6 +256,11 @@ int main(string[] args)
 			cache.addImportPaths(request.importPaths);
 		}
 
+		if (request.kind & RequestKind.removeImport)
+		{
+			cache.removeImportPaths(request.importPaths);
+		}
+
 		if (request.kind & RequestKind.listImports)
 		{
 			AutocompleteResponse response;
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index cf17c1c..ea1c0ca 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -4,6 +4,7 @@ GREEN="\033[32m"
 YELLOW="\033[33m"
 NORMAL="\033[0m"
 IMPORTS=$(pwd)/imports
+export IMPORTS
 
 fail_count=0
 pass_count=0
diff --git a/tests/tc_rm_import/expected1.txt b/tests/tc_rm_import/expected1.txt
new file mode 100644
index 0000000..7d65484
--- /dev/null
+++ b/tests/tc_rm_import/expected1.txt
@@ -0,0 +1,2 @@
+identifiers
+Point	s
diff --git a/tests/tc_rm_import/expected2.txt b/tests/tc_rm_import/expected2.txt
new file mode 100644
index 0000000..e69de29
diff --git a/tests/tc_rm_import/file.d b/tests/tc_rm_import/file.d
new file mode 100644
index 0000000..2646ddb
--- /dev/null
+++ b/tests/tc_rm_import/file.d
@@ -0,0 +1 @@
+import point:;
diff --git a/tests/tc_rm_import/run.sh b/tests/tc_rm_import/run.sh
new file mode 100755
index 0000000..74382c6
--- /dev/null
+++ b/tests/tc_rm_import/run.sh
@@ -0,0 +1,11 @@
+set -e
+set -u
+
+../../bin/dcd-client $1 file.d -c13 > actual1.txt
+diff actual1.txt expected1.txt
+../../bin/dcd-client $1 file.d -R$IMPORTS
+../../bin/dcd-client $1 file.d -c13 > actual2.txt
+diff actual2.txt expected2.txt
+../../bin/dcd-client $1 -I$IMPORTS
+../../bin/dcd-client $1 file.d -c13 > actual1.txt
+diff actual1.txt expected1.txt