Browse Source

Cleanup of Ctags and Grep classes

pipelines/143601543
eidheim 6 years ago
parent
commit
b5e5219a1c
  1. 48
      src/ctags.cc
  2. 10
      src/ctags.h
  3. 21
      src/grep.cc
  4. 10
      src/grep.h
  5. 17
      src/project.cc
  6. 13
      src/source_generic.cc
  7. 20
      src/window.cc
  8. 78
      tests/ctags_grep_test.cc

48
src/ctags.cc

@ -4,12 +4,9 @@
#include "project_build.h" #include "project_build.h"
#include "terminal.h" #include "terminal.h"
#include <climits> #include <climits>
#include <iostream>
#include <regex>
#include <vector> #include <vector>
std::pair<boost::filesystem::path, std::unique_ptr<std::stringstream>> Ctags::get_result(const boost::filesystem::path &path, bool enable_scope, bool enable_kind) { Ctags::Ctags(const boost::filesystem::path &path, bool enable_scope, bool enable_kind) : enable_scope(enable_scope), enable_kind(enable_kind) {
boost::filesystem::path run_path;
std::string fields(" --fields=n"); std::string fields(" --fields=n");
if(enable_scope) if(enable_scope)
fields += 's'; fields += 's';
@ -21,27 +18,32 @@ std::pair<boost::filesystem::path, std::unique_ptr<std::stringstream>> Ctags::ge
auto build = Project::Build::create(path); auto build = Project::Build::create(path);
std::string exclude = " --exclude=node_modules"; std::string exclude = " --exclude=node_modules";
if(!build->project_path.empty()) { if(!build->project_path.empty()) {
run_path = build->project_path; project_path = build->project_path;
exclude += " --exclude=" + filesystem::escape_argument(filesystem::get_relative_path(build->get_default_path(), run_path).string()); exclude += " --exclude=" + filesystem::escape_argument(filesystem::get_relative_path(build->get_default_path(), project_path).string());
exclude += " --exclude=" + filesystem::escape_argument(filesystem::get_relative_path(build->get_debug_path(), run_path).string()); exclude += " --exclude=" + filesystem::escape_argument(filesystem::get_relative_path(build->get_debug_path(), project_path).string());
} }
else else
run_path = path; project_path = path;
command = Config::get().project.ctags_command + exclude + fields + " --sort=foldcase -I \"override noexcept\" -f - -R *"; command = Config::get().project.ctags_command + exclude + fields + " --sort=foldcase -I \"override noexcept\" -f - -R *";
} }
else { else {
run_path = path.parent_path(); project_path = path.parent_path();
command = Config::get().project.ctags_command + fields + " --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; std::stringstream stdin_stream;
//TODO: when debian stable gets newer g++ version that supports move on streams, remove unique_ptr below Terminal::get().process(stdin_stream, output, command, project_path);
auto stdout_stream = std::make_unique<std::stringstream>();
Terminal::get().process(stdin_stream, *stdout_stream, command, run_path);
return {run_path, std::move(stdout_stream)};
} }
Ctags::Location Ctags::get_location(const std::string &line_, bool add_markup, bool scope_enabled, bool kind_enabled) { Ctags::operator bool() {
output.seekg(0, std::ios::end);
if(output.tellg() == 0)
return false;
output.seekg(0, std::ios::beg);
return true;
}
Ctags::Location Ctags::get_location(const std::string &line_, bool add_markup) const {
Location location; Location location;
#ifdef _WIN32 #ifdef _WIN32
@ -105,7 +107,7 @@ Ctags::Location Ctags::get_location(const std::string &line_, bool add_markup, b
} }
size_t line_start; size_t line_start;
if(kind_enabled) { if(enable_kind) {
auto kind_start = source_end + 4; auto kind_start = source_end + 4;
if(kind_start >= line.size()) { if(kind_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;
@ -135,7 +137,7 @@ Ctags::Location Ctags::get_location(const std::string &line_, bool add_markup, b
location.line = 0; location.line = 0;
} }
if(scope_enabled && line_end != std::string::npos && line_end + 1 < line.size()) { if(enable_scope && line_end != std::string::npos && line_end + 1 < line.size()) {
auto scope_start = line.find(':', line_end + 1); auto scope_start = line.find(':', line_end + 1);
if(scope_start != std::string::npos && scope_start + 1 < line.size()) if(scope_start != std::string::npos && scope_start + 1 < line.size())
location.scope = line.substr(scope_start + 1); location.scope = line.substr(scope_start + 1);
@ -188,11 +190,9 @@ std::vector<std::string> Ctags::get_type_parts(const std::string &type) {
} }
std::vector<Ctags::Location> Ctags::get_locations(const boost::filesystem::path &path, const std::string &name, const std::string &type) { std::vector<Ctags::Location> Ctags::get_locations(const boost::filesystem::path &path, const std::string &name, const std::string &type) {
auto result = get_result(path, true); Ctags ctags(path, true);
result.second->seekg(0, std::ios::end); if(!ctags)
if(result.second->tellg() == 0) return {};
return std::vector<Location>();
result.second->seekg(0, std::ios::beg);
//insert name into type //insert name into type
size_t c = 0; size_t c = 0;
@ -213,10 +213,10 @@ std::vector<Ctags::Location> Ctags::get_locations(const boost::filesystem::path
std::string line; std::string line;
long best_score = LONG_MIN; long best_score = LONG_MIN;
std::vector<Location> best_locations; std::vector<Location> best_locations;
while(std::getline(*result.second, line)) { while(std::getline(ctags.output, line)) {
if(line.size() > 2048) if(line.size() > 2048)
continue; continue;
auto location = Ctags::get_location(line, false, true); auto location = ctags.get_location(line);
if(!location.scope.empty()) { if(!location.scope.empty()) {
if(location.scope + "::" + location.symbol != name) if(location.scope + "::" + location.symbol != name)
continue; continue;
@ -224,7 +224,7 @@ std::vector<Ctags::Location> Ctags::get_locations(const boost::filesystem::path
else if(location.symbol != name) else if(location.symbol != name)
continue; continue;
location.file_path = result.first / location.file_path; location.file_path = ctags.project_path / location.file_path;
auto source_parts = get_type_parts(location.source); auto source_parts = get_type_parts(location.source);

10
src/ctags.h

@ -18,12 +18,18 @@ public:
operator bool() const { return !file_path.empty(); } 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, bool enable_scope = false, bool enable_kind = false); Ctags(const boost::filesystem::path &path, bool enable_scope = false, bool enable_kind = false);
static Location get_location(const std::string &line, bool add_markup, bool scope_enabled = false, bool kind_enabled = false); operator bool();
Location get_location(const std::string &line, bool add_markup = false) const;
boost::filesystem::path project_path;
std::stringstream output;
static std::vector<Location> get_locations(const boost::filesystem::path &path, const std::string &name, const std::string &type); static std::vector<Location> get_locations(const boost::filesystem::path &path, const std::string &name, const std::string &type);
private: private:
bool enable_scope, enable_kind;
static std::vector<std::string> get_type_parts(const std::string &type); static std::vector<std::string> get_type_parts(const std::string &type);
}; };

21
src/grep.cc

@ -4,17 +4,16 @@
#include "project_build.h" #include "project_build.h"
#include "terminal.h" #include "terminal.h"
std::pair<boost::filesystem::path, std::unique_ptr<std::stringstream>> Grep::get_result(const boost::filesystem::path &path, const std::string &pattern, bool case_sensitive, bool extended_regex) { Grep::Grep(const boost::filesystem::path &path, const std::string &pattern, bool case_sensitive, bool extended_regex) {
boost::filesystem::path run_path;
auto build = Project::Build::create(path); auto build = Project::Build::create(path);
std::string exclude = "--exclude-dir=node_modules"; std::string exclude = "--exclude-dir=node_modules";
if(!build->project_path.empty()) { if(!build->project_path.empty()) {
run_path = build->project_path; project_path = build->project_path;
exclude += " --exclude-dir=" + filesystem::escape_argument(filesystem::get_relative_path(build->get_default_path(), build->project_path).string()); exclude += " --exclude-dir=" + filesystem::escape_argument(filesystem::get_relative_path(build->get_default_path(), build->project_path).string());
exclude += " --exclude-dir=" + filesystem::escape_argument(filesystem::get_relative_path(build->get_debug_path(), build->project_path).string()); exclude += " --exclude-dir=" + filesystem::escape_argument(filesystem::get_relative_path(build->get_debug_path(), build->project_path).string());
} }
else else
run_path = path; project_path = path;
std::string flags; std::string flags;
if(!case_sensitive) if(!case_sensitive)
@ -34,12 +33,18 @@ std::pair<boost::filesystem::path, std::unique_ptr<std::stringstream>> Grep::get
std::stringstream stdin_stream; std::stringstream stdin_stream;
//TODO: when debian stable gets newer g++ version that supports move on streams, remove unique_ptr below //TODO: when debian stable gets newer g++ version that supports move on streams, remove unique_ptr below
auto stdout_stream = std::make_unique<std::stringstream>(); Terminal::get().process(stdin_stream, output, command, project_path);
Terminal::get().process(stdin_stream, *stdout_stream, command, run_path);
return {run_path, std::move(stdout_stream)};
} }
Grep::Location Grep::get_location(std::string line, bool color_codes_to_markup, bool include_offset, const std::string &only_for_file) { Grep::operator bool() {
output.seekg(0, std::ios::end);
if(output.tellg() == 0)
return false;
output.seekg(0, std::ios::beg);
return true;
}
Grep::Location Grep::get_location(std::string line, bool color_codes_to_markup, bool include_offset, const std::string &only_for_file) const {
std::vector<std::pair<size_t, size_t>> positions; std::vector<std::pair<size_t, size_t>> positions;
size_t file_end = std::string::npos, line_end = std::string::npos; size_t file_end = std::string::npos, line_end = std::string::npos;
if(color_codes_to_markup) { if(color_codes_to_markup) {

10
src/grep.h

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <sstream>
class Grep { class Grep {
public: public:
@ -12,7 +13,12 @@ public:
operator bool() const { return !file_path.empty(); } 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, const std::string &pattern, bool case_sensitive, bool extended_regex); Grep(const boost::filesystem::path &path, const std::string &pattern, bool case_sensitive, bool extended_regex);
static Location get_location(std::string line, bool color_codes_to_markup, bool include_offset, const std::string &only_for_file = {}); operator bool();
Location get_location(std::string line, bool color_codes_to_markup, bool include_offset, const std::string &only_for_file = {}) const;
boost::filesystem::path project_path;
std::stringstream output;
}; };

17
src/project.cc

@ -219,16 +219,11 @@ void Project::Base::recreate_build() {
} }
void Project::Base::show_symbols() { void Project::Base::show_symbols() {
auto pair = Ctags::get_result(get_preferably_view_folder()); Ctags ctags(get_preferably_view_folder());
if(!ctags) {
auto path = std::move(pair.first);
auto stream = std::move(pair.second);
stream->seekg(0, std::ios::end);
if(stream->tellg() == 0) {
Info::get().print("No symbols found in current project"); Info::get().print("No symbols found in current project");
return; return;
} }
stream->seekg(0, std::ios::beg);
auto view = Notebook::get().get_current_view(); auto view = Notebook::get().get_current_view();
if(view) if(view)
@ -239,8 +234,8 @@ void Project::Base::show_symbols() {
std::vector<Source::Offset> rows; std::vector<Source::Offset> rows;
std::string line; std::string line;
while(std::getline(*stream, line)) { while(std::getline(ctags.output, line)) {
auto location = Ctags::get_location(line, true); auto location = ctags.get_location(line, true);
std::string row = location.file_path.string() + ":" + std::to_string(location.line + 1) + ": " + location.source; std::string row = location.file_path.string() + ":" + std::to_string(location.line + 1) + ": " + location.source;
rows.emplace_back(Source::Offset(location.line, location.index, location.file_path)); rows.emplace_back(Source::Offset(location.line, location.index, location.file_path));
@ -249,11 +244,11 @@ void Project::Base::show_symbols() {
if(rows.size() == 0) if(rows.size() == 0)
return; return;
SelectionDialog::get()->on_select = [rows = std::move(rows), path = std::move(path)](unsigned int index, const std::string &text, bool hide_window) { SelectionDialog::get()->on_select = [rows = std::move(rows), project_path = std::move(ctags.project_path)](unsigned int index, const std::string &text, bool hide_window) {
if(index >= rows.size()) if(index >= rows.size())
return; return;
auto offset = rows[index]; auto offset = rows[index];
auto full_path = path / offset.file_path; auto full_path = project_path / offset.file_path;
boost::system::error_code ec; boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(full_path, ec)) if(!boost::filesystem::is_regular_file(full_path, ec))
return; return;

13
src/source_generic.cc

@ -82,22 +82,17 @@ Source::GenericView::GenericView(const boost::filesystem::path &file_path, const
else else
file_path = this->file_path; file_path = this->file_path;
auto pair = Ctags::get_result(file_path, false, true); Ctags ctags(file_path, false, true);
if(use_tmp_file) if(use_tmp_file)
boost::filesystem::remove_all(file_path.parent_path(), ec); boost::filesystem::remove_all(file_path.parent_path(), ec);
auto path = std::move(pair.first); if(!ctags) {
auto stream = std::move(pair.second);
stream->seekg(0, std::ios::end);
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;
} }
stream->seekg(0, std::ios::beg);
std::string line; std::string line;
while(std::getline(*stream, line)) { while(std::getline(ctags.output, line)) {
auto location = Ctags::get_location(line, true, false, true); auto location = ctags.get_location(line, true);
std::transform(location.kind.begin(), location.kind.end(), location.kind.begin(), std::transform(location.kind.begin(), location.kind.end(), location.kind.begin(),
[](char c) { return std::tolower(c); }); [](char c) { return std::tolower(c); });
std::vector<std::string> ignore_kinds = {"variable", "local", "constant", "global", "property", "member", "enum", std::vector<std::string> ignore_kinds = {"variable", "local", "constant", "global", "property", "member", "enum",

20
src/window.cc

@ -793,16 +793,12 @@ void Window::set_menu_actions() {
auto pattern = pattern_; // Store pattern to safely hide entrybox auto pattern = pattern_; // Store pattern to safely hide entrybox
EntryBox::get().hide(); EntryBox::get().hide();
if(!pattern.empty()) { if(!pattern.empty()) {
auto pair = Grep::get_result(Project::get_preferably_view_folder(), pattern, find_pattern_case_sensitive, find_pattern_extended_regex); auto grep = std::make_shared<Grep>(Project::get_preferably_view_folder(), pattern, find_pattern_case_sensitive, find_pattern_extended_regex);
auto path = std::move(pair.first); if(!*grep) {
auto stream = std::move(pair.second);
stream->seekg(0, std::ios::end);
if(stream->tellg() == 0) {
Info::get().print("Pattern not found"); Info::get().print("Pattern not found");
EntryBox::get().hide(); EntryBox::get().hide();
return; return;
} }
stream->seekg(0, std::ios::beg);
if(auto view = Notebook::get().get_current_view()) if(auto view = Notebook::get().get_current_view())
SelectionDialog::create(view, true, true); SelectionDialog::create(view, true, true);
@ -814,12 +810,12 @@ void Window::set_menu_actions() {
unsigned int current_line = 0; unsigned int current_line = 0;
auto view = Notebook::get().get_current_view(); auto view = Notebook::get().get_current_view();
if(view) { if(view) {
current_path = filesystem::get_relative_path(view->file_path, path).string(); current_path = filesystem::get_relative_path(view->file_path, grep->project_path).string();
current_line = view->get_buffer()->get_insert()->get_iter().get_line(); current_line = view->get_buffer()->get_insert()->get_iter().get_line();
} }
bool set_cursor_at_path = true; bool set_cursor_at_path = true;
while(std::getline(*stream, line)) { while(std::getline(grep->output, line)) {
auto location = Grep::get_location(std::move(line), true, false, current_path); auto location = grep->get_location(std::move(line), true, false, current_path);
SelectionDialog::get()->add_row(location.markup); SelectionDialog::get()->add_row(location.markup);
if(view && location) { if(view && location) {
if(set_cursor_at_path) { if(set_cursor_at_path) {
@ -832,9 +828,9 @@ void Window::set_menu_actions() {
} }
} }
} }
SelectionDialog::get()->on_select = [path = std::move(path)](unsigned int index, const std::string &text, bool hide_window) { SelectionDialog::get()->on_select = [grep](unsigned int index, const std::string &text, bool hide_window) {
auto location = Grep::get_location(text, false, true); auto location = grep->get_location(text, false, true);
Notebook::get().open(path / location.file_path); Notebook::get().open(grep->project_path / location.file_path);
auto view = Notebook::get().get_current_view(); auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_offset(location.line, location.offset); view->place_cursor_at_line_offset(location.line, location.offset);
view->scroll_to_cursor_delayed(true, false); view->scroll_to_cursor_delayed(true, false);

78
tests/ctags_grep_test.cc

@ -22,16 +22,16 @@ int main() {
// Ctags tests // Ctags tests
{ {
auto result = Ctags::get_result(tests_path); Ctags ctags(tests_path);
g_assert(result.first == tests_path.parent_path()); g_assert(ctags.project_path == tests_path.parent_path());
bool found = false; bool found = false;
std::string line; std::string line;
while(std::getline(*result.second, line)) { while(std::getline(ctags.output, line)) {
if(line.find("ctags_grep_test_function") != std::string::npos) { if(line.find("ctags_grep_test_function") != std::string::npos) {
{ {
auto location = Ctags::get_location(line, false); auto location = ctags.get_location(line, false);
g_assert(location.source == "void ctags_grep_test_function() {"); g_assert(location.source == "void ctags_grep_test_function() {");
g_assert(result.first / location.file_path == tests_path / "ctags_grep_test.cc"); g_assert(ctags.project_path / location.file_path == tests_path / "ctags_grep_test.cc");
g_assert_cmpint(location.line, ==, 6); g_assert_cmpint(location.line, ==, 6);
g_assert_cmpint(location.index, ==, 5); g_assert_cmpint(location.index, ==, 5);
g_assert(location.symbol == "ctags_grep_test_function"); g_assert(location.symbol == "ctags_grep_test_function");
@ -39,9 +39,9 @@ int main() {
g_assert(location.kind.empty()); g_assert(location.kind.empty());
} }
{ {
auto location = Ctags::get_location(line, true); auto location = ctags.get_location(line, true);
g_assert(location.source == "void <b>ctags_grep_test_function</b>() {"); g_assert(location.source == "void <b>ctags_grep_test_function</b>() {");
g_assert(result.first / location.file_path == tests_path / "ctags_grep_test.cc"); g_assert(ctags.project_path / location.file_path == tests_path / "ctags_grep_test.cc");
g_assert_cmpint(location.line, ==, 6); g_assert_cmpint(location.line, ==, 6);
g_assert_cmpint(location.index, ==, 5); g_assert_cmpint(location.index, ==, 5);
g_assert(location.symbol == "ctags_grep_test_function"); g_assert(location.symbol == "ctags_grep_test_function");
@ -55,16 +55,16 @@ int main() {
g_assert(found == true); g_assert(found == true);
} }
{ {
auto result = Ctags::get_result(tests_path, false, true); Ctags ctags(tests_path, false, true);
g_assert(result.first == tests_path.parent_path()); g_assert(ctags.project_path == tests_path.parent_path());
bool found = false; bool found = false;
std::string line; std::string line;
while(std::getline(*result.second, line)) { while(std::getline(ctags.output, line)) {
if(line.find("ctags_grep_test_function") != std::string::npos) { if(line.find("ctags_grep_test_function") != std::string::npos) {
{ {
auto location = Ctags::get_location(line, false, false, true); auto location = ctags.get_location(line, false);
g_assert(location.source == "void ctags_grep_test_function() {"); g_assert(location.source == "void ctags_grep_test_function() {");
g_assert(result.first / location.file_path == tests_path / "ctags_grep_test.cc"); g_assert(ctags.project_path / location.file_path == tests_path / "ctags_grep_test.cc");
g_assert_cmpint(location.line, ==, 6); g_assert_cmpint(location.line, ==, 6);
g_assert_cmpint(location.index, ==, 5); g_assert_cmpint(location.index, ==, 5);
g_assert(location.symbol == "ctags_grep_test_function"); g_assert(location.symbol == "ctags_grep_test_function");
@ -72,9 +72,9 @@ int main() {
g_assert(location.kind == "function"); g_assert(location.kind == "function");
} }
{ {
auto location = Ctags::get_location(line, true, false, true); auto location = ctags.get_location(line, true);
g_assert(location.source == "void <b>ctags_grep_test_function</b>() {"); g_assert(location.source == "void <b>ctags_grep_test_function</b>() {");
g_assert(result.first / location.file_path == tests_path / "ctags_grep_test.cc"); g_assert(ctags.project_path / location.file_path == tests_path / "ctags_grep_test.cc");
g_assert_cmpint(location.line, ==, 6); g_assert_cmpint(location.line, ==, 6);
g_assert_cmpint(location.index, ==, 5); g_assert_cmpint(location.index, ==, 5);
g_assert(location.symbol == "ctags_grep_test_function"); g_assert(location.symbol == "ctags_grep_test_function");
@ -88,16 +88,16 @@ int main() {
g_assert(found == true); g_assert(found == true);
} }
{ {
auto result = Ctags::get_result(tests_path, true); Ctags ctags(tests_path, true);
g_assert(result.first == tests_path.parent_path()); g_assert(ctags.project_path == tests_path.parent_path());
bool found = false; bool found = false;
std::string line; std::string line;
while(std::getline(*result.second, line)) { while(std::getline(ctags.output, line)) {
if(line.find("ctags_grep_test_function2") != std::string::npos) { if(line.find("ctags_grep_test_function2") != std::string::npos) {
{ {
auto location = Ctags::get_location(line, false, true); auto location = ctags.get_location(line, false);
g_assert(location.source == "void ctags_grep_test_function2() {"); g_assert(location.source == "void ctags_grep_test_function2() {");
g_assert(result.first / location.file_path == tests_path / "ctags_grep_test.cc"); g_assert(ctags.project_path / location.file_path == tests_path / "ctags_grep_test.cc");
g_assert_cmpint(location.line, ==, 10); g_assert_cmpint(location.line, ==, 10);
g_assert_cmpint(location.index, ==, 7); g_assert_cmpint(location.index, ==, 7);
g_assert(location.symbol == "ctags_grep_test_function2"); g_assert(location.symbol == "ctags_grep_test_function2");
@ -113,50 +113,50 @@ int main() {
// Grep tests // Grep tests
{ {
auto result = Grep::get_result(tests_path, "ctags_grep_test_function", true, false); Grep grep(tests_path, "ctags_grep_test_function", true, false);
g_assert(result.first == tests_path.parent_path()); g_assert(grep.project_path == tests_path.parent_path());
bool found = false; bool found = false;
std::string line; std::string line;
while(std::getline(*result.second, line)) { while(std::getline(grep.output, line)) {
if(line.find("ctags_grep_test_function") != std::string::npos) { if(line.find("ctags_grep_test_function") != std::string::npos) {
{ {
auto location = Grep::get_location(line, true, true); auto location = grep.get_location(line, true, true);
g_assert(location.markup == "tests/ctags_grep_test.cc:7:void <b>ctags_grep_test_function</b>() {"); g_assert(location.markup == "tests/ctags_grep_test.cc:7:void <b>ctags_grep_test_function</b>() {");
g_assert(result.first / location.file_path == tests_path / "ctags_grep_test.cc"); g_assert(grep.project_path / location.file_path == tests_path / "ctags_grep_test.cc");
g_assert_cmpint(location.line, ==, 6); g_assert_cmpint(location.line, ==, 6);
g_assert_cmpint(location.offset, ==, 5); g_assert_cmpint(location.offset, ==, 5);
{ {
auto location2 = Grep::get_location(location.markup, false, true); auto location2 = grep.get_location(location.markup, false, true);
g_assert(location2.markup == "tests/ctags_grep_test.cc:7:void <b>ctags_grep_test_function</b>() {"); g_assert(location2.markup == "tests/ctags_grep_test.cc:7:void <b>ctags_grep_test_function</b>() {");
g_assert(result.first / location2.file_path == tests_path / "ctags_grep_test.cc"); g_assert(grep.project_path / location2.file_path == tests_path / "ctags_grep_test.cc");
g_assert_cmpint(location2.line, ==, 6); g_assert_cmpint(location2.line, ==, 6);
g_assert_cmpint(location2.offset, ==, 5); g_assert_cmpint(location2.offset, ==, 5);
} }
} }
{ {
auto location = Grep::get_location(line, true, false); auto location = grep.get_location(line, true, false);
g_assert(location.markup == "tests/ctags_grep_test.cc:7:void <b>ctags_grep_test_function</b>() {"); g_assert(location.markup == "tests/ctags_grep_test.cc:7:void <b>ctags_grep_test_function</b>() {");
g_assert(result.first / location.file_path == tests_path / "ctags_grep_test.cc"); g_assert(grep.project_path / location.file_path == tests_path / "ctags_grep_test.cc");
g_assert_cmpint(location.line, ==, 6); g_assert_cmpint(location.line, ==, 6);
g_assert_cmpint(location.offset, ==, 0); g_assert_cmpint(location.offset, ==, 0);
{ {
auto location2 = Grep::get_location(location.markup, false, false); auto location2 = grep.get_location(location.markup, false, false);
g_assert(location2.markup == "tests/ctags_grep_test.cc:7:void <b>ctags_grep_test_function</b>() {"); g_assert(location2.markup == "tests/ctags_grep_test.cc:7:void <b>ctags_grep_test_function</b>() {");
g_assert(result.first / location2.file_path == tests_path / "ctags_grep_test.cc"); g_assert(grep.project_path / location2.file_path == tests_path / "ctags_grep_test.cc");
g_assert_cmpint(location2.line, ==, 6); g_assert_cmpint(location2.line, ==, 6);
g_assert_cmpint(location2.offset, ==, 0); g_assert_cmpint(location2.offset, ==, 0);
} }
} }
{ {
auto location = Grep::get_location(line, true, true, (boost::filesystem::path("tests") / "ctags_grep_test.cc").string()); auto location = grep.get_location(line, true, true, (boost::filesystem::path("tests") / "ctags_grep_test.cc").string());
g_assert(location.markup == "tests/ctags_grep_test.cc:7:void <b>ctags_grep_test_function</b>() {"); g_assert(location.markup == "tests/ctags_grep_test.cc:7:void <b>ctags_grep_test_function</b>() {");
g_assert(result.first / location.file_path == tests_path / "ctags_grep_test.cc"); g_assert(grep.project_path / location.file_path == tests_path / "ctags_grep_test.cc");
g_assert(location); g_assert(location);
g_assert_cmpint(location.line, ==, 6); g_assert_cmpint(location.line, ==, 6);
g_assert_cmpint(location.offset, ==, 5); g_assert_cmpint(location.offset, ==, 5);
} }
{ {
auto location = Grep::get_location(line, true, true, "CMakeLists.txt"); auto location = grep.get_location(line, true, true, "CMakeLists.txt");
g_assert(location.markup == "tests/ctags_grep_test.cc:7:void <b>ctags_grep_test_function</b>() {"); g_assert(location.markup == "tests/ctags_grep_test.cc:7:void <b>ctags_grep_test_function</b>() {");
g_assert(location.file_path.empty()); g_assert(location.file_path.empty());
g_assert(!location); g_assert(!location);
@ -172,22 +172,22 @@ int main() {
{ {
auto pattern = std::string("C") + "tags_grep_test_function"; auto pattern = std::string("C") + "tags_grep_test_function";
{ {
auto result = Grep::get_result(tests_path, pattern, true, false); Grep grep(tests_path, pattern, true, false);
g_assert(result.first == tests_path.parent_path()); g_assert(grep.project_path == tests_path.parent_path());
bool found = false; bool found = false;
std::string line; std::string line;
while(std::getline(*result.second, line)) { while(std::getline(grep.output, line)) {
if(line.find("ctags_grep_test_function") != std::string::npos) if(line.find("ctags_grep_test_function") != std::string::npos)
found = true; found = true;
} }
g_assert(found == false); g_assert(found == false);
} }
{ {
auto result = Grep::get_result(tests_path, pattern, false, false); Grep grep(tests_path, pattern, false, false);
g_assert(result.first == tests_path.parent_path()); g_assert(grep.project_path == tests_path.parent_path());
bool found = false; bool found = false;
std::string line; std::string line;
while(std::getline(*result.second, line)) { while(std::getline(grep.output, line)) {
if(line.find("ctags_grep_test_function") != std::string::npos) if(line.find("ctags_grep_test_function") != std::string::npos)
found = true; found = true;
} }

Loading…
Cancel
Save