Browse Source

Slight optimization of ctags, and more general goto methods when ctags is used

pipelines/143601543
eidheim 6 years ago
parent
commit
9346f0ee11
  1. 31
      src/ctags.cc
  2. 6
      src/ctags.h
  3. 23
      src/source_generic.cc

31
src/ctags.cc

@ -8,8 +8,9 @@
#include <regex>
#include <vector>
std::pair<boost::filesystem::path, std::unique_ptr<std::stringstream>> Ctags::get_result(const boost::filesystem::path &path) {
std::pair<boost::filesystem::path, std::unique_ptr<std::stringstream>> Ctags::get_result(const boost::filesystem::path &path, bool enable_kinds) {
boost::filesystem::path run_path;
auto fields = std::string(" --fields=ns") + (enable_kinds ? "K" : "");
std::string command;
boost::system::error_code ec;
if(boost::filesystem::is_directory(path, ec)) {
@ -22,11 +23,11 @@ std::pair<boost::filesystem::path, std::unique_ptr<std::stringstream>> Ctags::ge
}
else
run_path = path;
command = Config::get().project.ctags_command + exclude + " --fields=nsk --sort=foldcase -I \"override noexcept\" -f - -R *";
command = Config::get().project.ctags_command + exclude + fields + " --sort=foldcase -I \"override noexcept\" -f - -R *";
}
else {
run_path = path.parent_path();
command = Config::get().project.ctags_command + " --fields=nsk --sort=foldcase -I \"override noexcept\" -f - " + path.string();
command = Config::get().project.ctags_command + fields + " --sort=foldcase -I \"override noexcept\" -f - " + path.string();
}
std::stringstream stdin_stream;
@ -36,7 +37,7 @@ std::pair<boost::filesystem::path, std::unique_ptr<std::stringstream>> Ctags::ge
return {run_path, std::move(stdout_stream)};
}
Ctags::Location Ctags::get_location(const std::string &line_, bool add_markup) {
Ctags::Location Ctags::get_location(const std::string &line_, bool add_markup, bool kinds_enabled) {
Location location;
#ifdef _WIN32
@ -87,14 +88,24 @@ Ctags::Location Ctags::get_location(const std::string &line_, bool add_markup) {
}
location.source = line.substr(source_start, source_end - source_start - (line[source_end - 1] == '$' ? 1 : 0));
auto kind_start = source_end + 4;
if(kind_start >= line.size()) {
std::cerr << "Warning (ctags): could not parse line: " << line << std::endl;
return location;
size_t line_start;
if(kinds_enabled) {
auto kind_start = source_end + 4;
if(kind_start >= line.size()) {
std::cerr << "Warning (ctags): could not parse line: " << line << std::endl;
return location;
}
auto kind_end = line.find('\t', kind_start);
if(kind_end == std::string::npos) {
std::cerr << "Warning (ctags): could not parse line: " << line << std::endl;
return location;
}
location.kind = line.substr(kind_start, kind_end - kind_start);
line_start = kind_start + location.kind.size() + 6;
}
location.kind = line[kind_start];
else
line_start = source_end + 9;
auto line_start = kind_start + 7;
if(line_start >= line.size()) {
std::cerr << "Warning (ctags): could not parse line: " << line << std::endl;
return location;

6
src/ctags.h

@ -14,13 +14,13 @@ public:
std::string symbol;
std::string scope;
std::string source;
char kind;
std::string kind;
operator bool() const { return !file_path.empty(); }
};
static std::pair<boost::filesystem::path, std::unique_ptr<std::stringstream>> get_result(const boost::filesystem::path &path);
static std::pair<boost::filesystem::path, std::unique_ptr<std::stringstream>> get_result(const boost::filesystem::path &path, bool enable_kinds = false);
static Location get_location(const std::string &line, bool add_markup);
static Location get_location(const std::string &line, bool add_markup, bool kinds_enabled = false);
static std::vector<Location> get_locations(const boost::filesystem::path &path, const std::string &name, const std::string &type);

23
src/source_generic.cc

@ -70,15 +70,21 @@ Source::GenericView::GenericView(const boost::filesystem::path &file_path, const
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());
file_path /= "jucipp_get_methods" + std::to_string(get_current_process_id());
boost::filesystem::create_directory(file_path, ec);
if(ec) {
Terminal::get().print("Error: could not create temporary folder\n", true);
return methods;
}
file_path /= this->file_path.filename();
filesystem::write(file_path, this->get_buffer()->get_text().raw());
}
else
file_path = this->file_path;
auto pair = Ctags::get_result(file_path);
auto pair = Ctags::get_result(file_path, true);
if(use_tmp_file)
boost::filesystem::remove(file_path, ec);
boost::filesystem::remove_all(file_path.parent_path(), ec);
auto path = std::move(pair.first);
auto stream = std::move(pair.second);
stream->seekg(0, std::ios::end);
@ -90,10 +96,15 @@ Source::GenericView::GenericView(const boost::filesystem::path &file_path, const
stream->seekg(0, std::ios::beg);
std::string line;
bool all_kinds = this->language && (this->language->get_id() == "markdown" || this->language->get_id() == "json");
while(std::getline(*stream, line)) {
auto location = Ctags::get_location(line, true);
if(all_kinds || location.kind == 'f' || location.kind == 's' || location.kind == 'm')
auto location = Ctags::get_location(line, true, true);
std::transform(location.kind.begin(), location.kind.end(), location.kind.begin(),
[](char c) { return std::tolower(c); });
std::vector<std::string> ignore_kinds = {"variable", "local", "constant", "global", "property", "member", "enum",
"macro", "param", "header",
"typedef", "using", "alias",
"project", "option"};
if(std::none_of(ignore_kinds.begin(), ignore_kinds.end(), [&location](const std::string &e) { return location.kind.find(e) != std::string::npos; }))
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) {

Loading…
Cancel
Save