mirror of https://github.com/buggins/dlangide.git
Cleaned up Go To Definition implemention
This commit is contained in:
parent
736517018f
commit
747cb61fee
|
@ -1,17 +1,78 @@
|
||||||
module dlangide.tools.d.DCDInterface;
|
module dlangide.tools.d.DCDInterface;
|
||||||
|
|
||||||
|
import dlangui.core.logger;
|
||||||
|
|
||||||
import dlangide.builders.extprocess;
|
import dlangide.builders.extprocess;
|
||||||
|
|
||||||
|
import std.typecons;
|
||||||
|
import std.conv;
|
||||||
|
import std.string;
|
||||||
|
|
||||||
|
enum DCDResult : int {
|
||||||
|
DCD_NOT_RUNNING = 0,
|
||||||
|
SUCCESS,
|
||||||
|
NO_RESULT,
|
||||||
|
FAIL,
|
||||||
|
}
|
||||||
|
alias ResultSet = Tuple!(DCDResult, "result", dstring[], "output");
|
||||||
|
|
||||||
//Interface to DCD
|
//Interface to DCD
|
||||||
//TODO: Check if server is running, start server if needed etc.
|
//TODO: Check if server is running, start server if needed etc.
|
||||||
class DCDInterface {
|
class DCDInterface {
|
||||||
ExternalProcess dcdProcess;
|
ExternalProcess dcdProcess;
|
||||||
|
ProtectedTextStorage stdoutTarget;
|
||||||
this() {
|
this() {
|
||||||
dcdProcess = new ExternalProcess();
|
dcdProcess = new ExternalProcess();
|
||||||
|
stdoutTarget = new ProtectedTextStorage();
|
||||||
}
|
}
|
||||||
bool execute(char[][] arguments ,ref dstring output, dstring input) {
|
|
||||||
ProtectedTextStorage stdoutTarget = new ProtectedTextStorage();
|
ResultSet goToDefinition(in dstring content, int index) {
|
||||||
ExternalProcess dcdProcess = new ExternalProcess();
|
ExternalProcess dcdProcess = new ExternalProcess();
|
||||||
|
|
||||||
|
ResultSet result;
|
||||||
|
if(dcdProcess.state != ExternalProcessState.None) {
|
||||||
|
result.result = DCDResult.FAIL;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
char[][] arguments = ["-l".dup, "-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) {
|
||||||
|
if(output[0].indexOf("Not Found".dup) == 0) {
|
||||||
|
result.result = DCDResult.NO_RESULT;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto split = output[0].indexOf("\t");
|
||||||
|
if(split == -1) {
|
||||||
|
Log.d("DCD output format error.");
|
||||||
|
result.result = DCDResult.FAIL;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.output ~= output[0][0 .. split];
|
||||||
|
result.output ~= output[0][split+1 .. $];
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool execute(char[][] arguments ,ref dstring output, dstring input) {
|
||||||
//TODO: Working Directory, where is that?
|
//TODO: Working Directory, where is that?
|
||||||
//TODO: Inform user when dcd-client is not available.
|
//TODO: Inform user when dcd-client is not available.
|
||||||
dcdProcess.run("dcd-client".dup, arguments, "/usr/bin".dup, stdoutTarget);
|
dcdProcess.run("dcd-client".dup, arguments, "/usr/bin".dup, stdoutTarget);
|
||||||
|
|
|
@ -22,46 +22,33 @@ class DEditorTool : EditorTool
|
||||||
|
|
||||||
override bool goToDefinition(DSourceEdit editor, TextPosition caretPosition) {
|
override bool goToDefinition(DSourceEdit editor, TextPosition caretPosition) {
|
||||||
|
|
||||||
|
auto byteOffset = caretPositionToByteOffset(editor.text, caretPosition);
|
||||||
|
ResultSet output = _dcd.goToDefinition(editor.text, byteOffset);
|
||||||
|
|
||||||
auto content = editor.text();
|
|
||||||
auto byteOffset = caretPositionToByteOffset(content, caretPosition);
|
|
||||||
|
|
||||||
char[][] arguments = ["-l".dup, "-c".dup];
|
switch(output.result) {
|
||||||
arguments ~= [to!(char[])(byteOffset)];
|
//TODO: Show dialog
|
||||||
//arguments ~= [to!(char[])(editor.projectSourceFile.filename())];
|
case DCDResult.FAIL:
|
||||||
|
case DCDResult.DCD_NOT_RUNNING:
|
||||||
dstring output;
|
case DCDResult.NO_RESULT:
|
||||||
_dcd.execute(arguments, output, content);
|
return false;
|
||||||
|
case DCDResult.SUCCESS:
|
||||||
string[] outputLines = to!string(output).splitLines();
|
auto target = to!int(output.output[1]);
|
||||||
Log.d("DCD:", outputLines);
|
if(output.output[0].indexOf("stdin".dup) != -1) {
|
||||||
|
Log.d("Declaration is in current file. Jumping to it.");
|
||||||
foreach(string outputLine ; outputLines) {
|
auto destPos = byteOffsetToCaret(editor.text, target);
|
||||||
if(outputLine.indexOf("Not Found".dup) == -1) {
|
|
||||||
auto split = outputLine.indexOf("\t");
|
|
||||||
if(split == -1) {
|
|
||||||
Log.d("DCD output format error.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if(indexOf(outputLine[0 .. split],"stdin".dup) != -1) {
|
|
||||||
Log.d("Declaration is in current file. Can jump to it.");
|
|
||||||
auto target = to!int(outputLine[split+1 .. $]);
|
|
||||||
auto destPos = byteOffsetToCaret(content, target);
|
|
||||||
editor.setCaretPos(destPos.line,destPos.pos);
|
editor.setCaretPos(destPos.line,destPos.pos);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
auto filename = outputLine[0 .. split];
|
//Must open file first to get the content for finding the correct caret position.
|
||||||
if(_frame !is null) {
|
_frame.openSourceFile(to!string(output.output[0]));
|
||||||
_frame.openSourceFile(filename);
|
|
||||||
auto target = to!int(outputLine[split+1 .. $]);
|
|
||||||
auto destPos = byteOffsetToCaret(_frame.currentEditor.text(), target);
|
auto destPos = byteOffsetToCaret(_frame.currentEditor.text(), target);
|
||||||
|
|
||||||
_frame.currentEditor.setCaretPos(destPos.line,destPos.pos);
|
_frame.currentEditor.setCaretPos(destPos.line,destPos.pos);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override dstring[] getCompletions(DSourceEdit editor, TextPosition caretPosition) {
|
override dstring[] getCompletions(DSourceEdit editor, TextPosition caretPosition) {
|
||||||
|
|
Loading…
Reference in New Issue