1 module served.commands.definition; 2 3 import served.extension; 4 import served.types; 5 import served.utils.ddoc; 6 7 import workspaced.api; 8 import workspaced.com.dcd; 9 import workspaced.coms; 10 11 import std.path : buildPath, isAbsolute; 12 13 import fs = std.file; 14 import io = std.stdio; 15 16 @protocolMethod("textDocument/definition") 17 ArrayOrSingle!Location provideDefinition(TextDocumentPositionParams params) 18 { 19 auto instance = activeInstance = backend.getBestInstance!DCDComponent( 20 params.textDocument.uri.uriToFile); 21 if (!instance) 22 return ArrayOrSingle!Location.init; 23 24 auto document = documents[params.textDocument.uri]; 25 if (document.languageId != "d") 26 return ArrayOrSingle!Location.init; 27 28 auto result = instance.get!DCDComponent.findDeclaration(document.rawText, 29 cast(int) document.positionToBytes(params.position)).getYield; 30 if (result == DCDDeclaration.init) 31 return ArrayOrSingle!Location.init; 32 33 auto uri = document.uri; 34 if (result.file != "stdin") 35 { 36 if (isAbsolute(result.file)) 37 uri = uriFromFile(result.file); 38 else 39 uri = null; 40 } 41 size_t byteOffset = cast(size_t) result.position; 42 Position pos; 43 auto found = documents.tryGet(uri); 44 if (found.uri) 45 pos = found.bytesToPosition(byteOffset); 46 else 47 { 48 string abs = result.file; 49 if (!abs.isAbsolute) 50 abs = buildPath(instance.cwd, abs); 51 pos = Position.init; 52 size_t totalLen; 53 foreach (line; io.File(abs).byLine(io.KeepTerminator.yes)) 54 { 55 totalLen += line.length; 56 if (totalLen >= byteOffset) 57 break; 58 else 59 pos.line++; 60 } 61 } 62 return ArrayOrSingle!Location(Location(uri, TextRange(pos, pos))); 63 } 64 65 @protocolMethod("textDocument/hover") 66 Hover provideHover(TextDocumentPositionParams params) 67 { 68 string file = params.textDocument.uri.uriToFile; 69 70 if (!backend.hasBest!DCDComponent(file)) 71 return Hover.init; 72 73 auto document = documents[params.textDocument.uri]; 74 if (document.languageId != "d") 75 return Hover.init; 76 77 auto docs = backend.best!DCDComponent(file).getDocumentation(document.rawText, 78 cast(int) document.positionToBytes(params.position)).getYield; 79 Hover ret; 80 ret.contents = docs.ddocToMarked; 81 return ret; 82 }