Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement "jumping to declaration" on irony-server. #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/src/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Command *CommandParser::parse(const std::vector<std::string> &argv) {
break;

case Command::Complete:
case Command::FindDeclaration:
positionalArgs.push_back(StringConverter(&command_.file));
positionalArgs.push_back(UnsignedIntConverter(&command_.line));
positionalArgs.push_back(UnsignedIntConverter(&command_.column));
Expand Down
3 changes: 3 additions & 0 deletions server/src/Commands.def
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ X(Diagnostics, "diagnostics", "FILE - look for diagnostics in file")
X(Complete,
"complete",
"FILE LINE COL - perform code completion at a given location")
X(FindDeclaration,
"find-declaration",
"FILE LINE COL - find the declaration of the symbol at a given location")
X(Help, "help", "show this message")
X(Exit, "exit", "exit interactive mode, print nothing")
X(SetDebug, "set-debug", "[on|off] - enable or disable verbose logging")
Expand Down
37 changes: 37 additions & 0 deletions server/src/Irony.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,40 @@ void Irony::complete(const std::string &file,
std::cout << ")\n";
}
}

void Irony::findDeclaration(const std::string &file,
unsigned line,
unsigned col,
const std::vector<std::string> &flags,
const std::vector<CXUnsavedFile> &unsavedFiles) {

CXTranslationUnit tu = tuManager_.getOrCreateTU(file, flags, unsavedFiles);

if (tu == nullptr) {
std::cout << "nil\n";
return;
}

// To get the location of the declaration, first get the current
// CXSourceLocation object. From there we get the cursor of the symbol under
// cursor. Next, use clang_getCursorReferenced to get the cursor of the
// declaration, and get the location of the declaration cursor.
CXFile f = clang_getFile(tu, file.data());
CXSourceLocation location = clang_getLocation(tu, f, line, col);
CXCursor cursor = clang_getCursor(tu, location);
cursor = clang_getCursorReferenced(cursor);
if (clang_Cursor_isNull(cursor)) { // no declaration found
std::cout << "nil" << std::endl;
return;
}
location = clang_getCursorLocation(cursor);

clang_getSpellingLocation(location, &f, &line, &col, NULL);
CXString file_name = clang_getFileName(f);

// print the file path, line number, and col number of the declaration
std::cout << "(\"" << clang_getCString(file_name) << '"' << ' ' <<
line << ' ' << col << ')' << std::endl;

clang_disposeString(file_name);
}
6 changes: 6 additions & 0 deletions server/src/Irony.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ class Irony {
unsigned col,
const std::vector<std::string> &flags,
const std::vector<CXUnsavedFile> &unsavedFiles);

void findDeclaration(const std::string &file,
unsigned line,
unsigned col,
const std::vector<std::string> &flags,
const std::vector<CXUnsavedFile> &unsavedFiles);
/// @}

private:
Expand Down
4 changes: 4 additions & 0 deletions server/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ int main(int ac, const char *av[]) {
irony.complete(c->file, c->line, c->column, c->flags, c->cxUnsavedFiles);
break;

case Command::FindDeclaration:
irony.findDeclaration(c->file, c->line, c->column, c->flags, c->cxUnsavedFiles);
break;

case Command::Exit:
return 0;

Expand Down