Browse Source

Fallback Go to Method now supported by all languages parsed by ctags. Additionally, if buffer is changed, a temporary file is created for ctags.

merge-requests/399/head
eidheim 6 years ago
parent
commit
68156d7ef2
  1. 13
      src/ctags.cc
  2. 1
      src/ctags.h
  3. 41
      src/source_generic.cc
  4. 4
      src/usages_clang.cc

13
src/ctags.cc

@ -22,11 +22,11 @@ std::pair<boost::filesystem::path, std::unique_ptr<std::stringstream>> Ctags::ge
} }
else else
run_path = path; run_path = path;
command = Config::get().project.ctags_command + exclude + " --fields=ns --sort=foldcase -I \"override noexcept\" -f - -R *"; command = Config::get().project.ctags_command + exclude + " --fields=nsk --sort=foldcase -I \"override noexcept\" -f - -R *";
} }
else { else {
run_path = path.parent_path(); run_path = path.parent_path();
command = Config::get().project.ctags_command + " --fields=ns --sort=foldcase -I \"override noexcept\" -f - " + path.string(); command = Config::get().project.ctags_command + " --fields=nsk --sort=foldcase -I \"override noexcept\" -f - " + path.string();
} }
std::stringstream stdin_stream; std::stringstream stdin_stream;
@ -87,7 +87,14 @@ Ctags::Location Ctags::get_location(const std::string &line_, bool markup) {
} }
location.source = line.substr(source_start, source_end - source_start - (line[source_end - 1] == '$' ? 1 : 0)); location.source = line.substr(source_start, source_end - source_start - (line[source_end - 1] == '$' ? 1 : 0));
auto line_start = source_end + 9; auto kind_start = source_end + 4;
if(kind_start >= line.size()) {
std::cerr << "Warning (ctags): could not parse line: " << line << std::endl;
return location;
}
location.kind = line[kind_start];
auto line_start = kind_start + 7;
if(line_start >= line.size()) { if(line_start >= line.size()) {
std::cerr << "Warning (ctags): could not parse line: " << line << std::endl; std::cerr << "Warning (ctags): could not parse line: " << line << std::endl;
return location; return location;

1
src/ctags.h

@ -14,6 +14,7 @@ public:
std::string symbol; std::string symbol;
std::string scope; std::string scope;
std::string source; std::string source;
char kind;
operator bool() const { return !file_path.empty(); } operator bool() const { return !file_path.empty(); }
}; };

41
src/source_generic.cc

@ -1,11 +1,24 @@
#include "source_generic.h" #include "source_generic.h"
#include "ctags.h" #include "ctags.h"
#include "filesystem.h"
#include "info.h" #include "info.h"
#include "selection_dialog.h" #include "selection_dialog.h"
#include "snippets.h" #include "snippets.h"
#include "terminal.h" #include "terminal.h"
#include <algorithm> #include <algorithm>
#ifdef _WIN32
#include <windows.h>
inline DWORD get_current_process_id() {
return GetCurrentProcessId();
}
#else
#include <unistd.h>
inline pid_t get_current_process_id() {
return getpid();
}
#endif
Source::GenericView::GenericView(const boost::filesystem::path &file_path, const Glib::RefPtr<Gsv::Language> &language) : BaseView(file_path, language), View(file_path, language, true), autocomplete(this, interactive_completion, last_keyval, false) { Source::GenericView::GenericView(const boost::filesystem::path &file_path, const Glib::RefPtr<Gsv::Language> &language) : BaseView(file_path, language), View(file_path, language, true), autocomplete(this, interactive_completion, last_keyval, false) {
configure(); configure();
spellcheck_all = true; spellcheck_all = true;
@ -43,14 +56,32 @@ Source::GenericView::GenericView(const boost::filesystem::path &file_path, const
setup_autocomplete(); setup_autocomplete();
if(language && (language->get_id() == "markdown" || language->get_id() == "json")) {
get_methods = [this]() { get_methods = [this]() {
auto pair = Ctags::get_result(this->file_path); std::vector<std::pair<Offset, std::string>> methods;
boost::filesystem::path file_path;
boost::system::error_code ec;
bool use_tmp_file = false;
if(this->get_buffer()->get_modified()) {
use_tmp_file = true;
file_path = boost::filesystem::temp_directory_path(ec);
if(ec) {
Terminal::get().print("Error: could not get temporary directory folder\n", true);
return methods;
}
file_path /= ("jucipp_get_methods" + std::to_string(get_current_process_id()) + this->file_path.filename().string());
filesystem::write(file_path, this->get_buffer()->get_text().raw());
}
else
file_path = this->file_path;
auto pair = Ctags::get_result(file_path);
if(use_tmp_file)
boost::filesystem::remove(file_path, ec);
auto path = std::move(pair.first); auto path = std::move(pair.first);
auto stream = std::move(pair.second); auto stream = std::move(pair.second);
stream->seekg(0, std::ios::end); stream->seekg(0, std::ios::end);
std::vector<std::pair<Offset, std::string>> methods;
if(stream->tellg() == 0) { if(stream->tellg() == 0) {
Info::get().print("No methods found in current buffer"); Info::get().print("No methods found in current buffer");
return methods; return methods;
@ -58,9 +89,10 @@ Source::GenericView::GenericView(const boost::filesystem::path &file_path, const
stream->seekg(0, std::ios::beg); stream->seekg(0, std::ios::beg);
std::string line; std::string line;
auto filename = this->file_path.filename(); bool all_kinds = this->language && (this->language->get_id() == "markdown" || this->language->get_id() == "json");
while(std::getline(*stream, line)) { while(std::getline(*stream, line)) {
auto location = Ctags::get_location(line, true); auto location = Ctags::get_location(line, true);
if(all_kinds || location.kind == 'f' || location.kind == 's' || location.kind == 'm')
methods.emplace_back(Offset(location.line, location.index), location.source); methods.emplace_back(Offset(location.line, location.index), location.source);
} }
std::sort(methods.begin(), methods.end(), [](const std::pair<Offset, std::string> &e1, const std::pair<Offset, std::string> &e2) { std::sort(methods.begin(), methods.end(), [](const std::pair<Offset, std::string> &e1, const std::pair<Offset, std::string> &e2) {
@ -71,7 +103,6 @@ Source::GenericView::GenericView(const boost::filesystem::path &file_path, const
Info::get().print("No methods found in current buffer"); Info::get().print("No methods found in current buffer");
return methods; return methods;
}; };
}
} }
Source::GenericView::~GenericView() { Source::GenericView::~GenericView() {

4
src/usages_clang.cc

@ -11,12 +11,12 @@
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
DWORD get_current_process_id() { inline DWORD get_current_process_id() {
return GetCurrentProcessId(); return GetCurrentProcessId();
} }
#else #else
#include <unistd.h> #include <unistd.h>
pid_t get_current_process_id() { inline pid_t get_current_process_id() {
return getpid(); return getpid();
} }
#endif #endif

Loading…
Cancel
Save