diff --git a/src/dlangide/tools/d/DCDInterface.d b/src/dlangide/tools/d/DCDInterface.d index 5fffb4c..441a927 100644 --- a/src/dlangide/tools/d/DCDInterface.d +++ b/src/dlangide/tools/d/DCDInterface.d @@ -72,6 +72,60 @@ class DCDInterface { return result; } + ResultSet getCompletions(in dstring content, int index) { + ExternalProcess dcdProcess = new ExternalProcess(); + + ResultSet result; + if(dcdProcess.state != ExternalProcessState.None) { + result.result = DCDResult.FAIL; + return result; + } + + char[][] arguments = ["-c".dup]; + arguments ~= [to!(char[])(index)]; + ProtectedTextStorage stdoutTarget = new ProtectedTextStorage(); + + dcdProcess.run("dcd-client".dup, arguments, "/usr/bin".dup, stdoutTarget); + dcdProcess.write(content); + dcdProcess.wait(); + + dstring[] output = stdoutTarget.readText.splitLines(); + + if(dcdProcess.poll() == ExternalProcessState.Stopped) { + result.result = DCDResult.SUCCESS; + } + else { + result.result = DCDResult.FAIL; + return result; + } + + if(output.length == 0) { + result.result = DCDResult.NO_RESULT; + return result; + } + + enum State : int {None = 0, Identifiers, Calltips} + State state = State.None; + foreach(dstring outputLine ; output) { + if(outputLine == "identifiers") { + state = State.Identifiers; + } + else if(outputLine == "calltips") { + state = State.Calltips; + } + else { + auto split = outputLine.indexOf("\t"); + if(split < 0) { + break; + } + if(state == State.Identifiers) { + result.output ~= outputLine[0 .. split]; + } + } + } + return result; + } + bool execute(char[][] arguments ,ref dstring output, dstring input) { //TODO: Working Directory, where is that? //TODO: Inform user when dcd-client is not available. diff --git a/src/dlangide/tools/d/DEditorTool.d b/src/dlangide/tools/d/DEditorTool.d index 86e9bc2..f866b4c 100644 --- a/src/dlangide/tools/d/DEditorTool.d +++ b/src/dlangide/tools/d/DEditorTool.d @@ -52,39 +52,20 @@ class DEditorTool : EditorTool } override dstring[] getCompletions(DSourceEdit editor, TextPosition caretPosition) { - auto content = editor.text(); - auto byteOffset = caretPositionToByteOffset(content, caretPosition); - char[][] arguments = ["-c".dup]; - arguments ~= [to!(char[])(byteOffset)]; - //arguments ~= [to!(char[])(editor.projectSourceFile.filename())]; - - dstring output; - _dcd.execute(arguments, output, content); - - char[] state = "".dup; - dstring[] suggestions; - foreach(dstring outputLine ; output.splitLines()) { - if(outputLine == "identifiers") { - state = "identifiers".dup; - } - else if(outputLine == "calltips") { - state = "calltips".dup; - } - else { - auto split = outputLine.indexOf("\t"); - if(split < 0) { - break; - } - if(state == "identifiers") { - suggestions ~= outputLine[0 .. split]; - } - } + auto byteOffset = caretPositionToByteOffset(editor.text, caretPosition); + ResultSet output = _dcd.getCompletions(editor.text, byteOffset); + switch(output.result) { + //TODO: Show dialog + case DCDResult.FAIL: + case DCDResult.DCD_NOT_RUNNING: + case DCDResult.NO_RESULT: + case DCDResult.SUCCESS: + default: + return output.output; } - return suggestions; } - private: DCDInterface _dcd;