Browse Source

Improved Source->Find Documentation

merge-requests/365/head
eidheim 8 years ago
parent
commit
2313445f8d
  1. 1
      src/CMakeLists.txt
  2. 12206
      src/documentation_cppreference.cc
  3. 9
      src/documentation_cppreference.h
  4. 121
      src/source_clang.cc
  5. 22
      src/window.cc

1
src/CMakeLists.txt

@ -5,6 +5,7 @@ set(JUCI_SHARED_FILES
compile_commands.cc compile_commands.cc
ctags.cc ctags.cc
dispatcher.cc dispatcher.cc
documentation_cppreference.cc
filesystem.cc filesystem.cc
git.cc git.cc
menu.cc menu.cc

12206
src/documentation_cppreference.cc

File diff suppressed because it is too large Load Diff

9
src/documentation_cppreference.h

@ -0,0 +1,9 @@
#pragma once
#include <string>
namespace Documentation {
class CppReference {
public:
static std::string get_url(const std::string symbol) noexcept;
};
}

121
src/source_clang.cc

@ -12,6 +12,7 @@
#include "filesystem.h" #include "filesystem.h"
#include "compile_commands.h" #include "compile_commands.h"
#include "usages_clang.h" #include "usages_clang.h"
#include "documentation_cppreference.h"
clangmm::Index Source::ClangViewParse::clang_index(0, 0); clangmm::Index Source::ClangViewParse::clang_index(0, 0);
@ -1481,107 +1482,63 @@ Source::ClangViewRefactor::ClangViewRefactor(const boost::filesystem::path &file
return methods; return methods;
}; };
get_token_data=[this]() { get_token_data=[this]() -> std::vector<std::string> {
const auto find_non_word_char=[](const std::string &str, size_t start_pos) { clangmm::Cursor cursor;
for(size_t c=start_pos;c<str.size();c++) {
if(!((str[c]>='a' && str[c]<='z') ||
(str[c]>='A' && str[c]<='Z') ||
(str[c]>='0' && str[c]<='9') ||
str[c]=='_'))
return c;
}
return std::string::npos;
};
std::vector<std::string> data; std::vector<std::string> data;
if(!parsed) { if(!parsed) {
Info::get().print("Buffer is parsing"); if(selected_completion_string) {
cursor=clangmm::CompletionString(selected_completion_string).get_cursor(clang_tu->cx_tu);
if(!cursor) {
Info::get().print("No symbol found");
return data; return data;
} }
auto iter=get_buffer()->get_insert()->get_iter();
auto line=static_cast<unsigned>(iter.get_line());
auto index=static_cast<unsigned>(iter.get_line_index());
for(size_t c=0;c<clang_tokens->size();++c) {
auto &token=(*clang_tokens)[c];
if(token.is_identifier()) {
auto &token_offsets=clang_tokens_offsets[c];
if(line==token_offsets.first.line-1 && index>=token_offsets.first.index-1 && index <=token_offsets.second.index-1) {
auto referenced=token.get_cursor().get_referenced();
if(referenced) {
auto usr=referenced.get_usr();
data.emplace_back("clang");
//namespace
size_t pos1, pos2=0;
while((pos1=usr.find('@', pos2))!=std::string::npos && pos1+1<usr.size() && usr[pos1+1]=='N') {
pos1+=3;
pos2=find_non_word_char(usr, pos1);
if(pos2!=std::string::npos) {
auto ns=usr.substr(pos1, pos2-pos1);
if(ns=="__1")
break;
data.emplace_back(ns);
}
else
break;
} }
if(data.size()==1)
data.emplace_back("");
//template arguments
size_t template_pos=usr.find('$');
bool found_type_or_function=false;
//type
pos2=0;
while(((pos1=usr.find("T@", pos2))!=std::string::npos || (pos1=usr.find("S@", pos2))!=std::string::npos) && pos1<template_pos) {
found_type_or_function=true;
pos1+=2;
pos2=find_non_word_char(usr, pos1);
if(pos2!=std::string::npos)
data.emplace_back(usr.substr(pos1, pos2-pos1));
else { else {
data.emplace_back(usr.substr(pos1)); Info::get().print("Buffer is parsing");
break; return data;
} }
} }
//function/constant//variable if(!cursor) {
pos1=usr.find("F@"); auto identifier=get_identifier();
if(pos1==std::string::npos) if(identifier) {
pos1=usr.find("C@"); cursor=identifier.cursor.get_definition();
if(pos1==std::string::npos) if(!cursor)
pos1=usr.find("I@"); cursor=identifier.cursor.get_canonical();
if(pos1!=std::string::npos) {
pos1+=2;
pos2=find_non_word_char(usr, pos1);
} }
if(pos1!=std::string::npos) {
found_type_or_function=true;
if(pos2!=std::string::npos)
data.emplace_back(usr.substr(pos1, pos2-pos1));
else
data.emplace_back(usr.substr(pos1));
} }
//Sometimes a method is at end without a identifier it seems: if(cursor) {
if(!found_type_or_function && (pos1=usr.rfind('@'))!=std::string::npos) { data.emplace_back("clang");
pos1++;
pos2=find_non_word_char(usr, pos1); std::string symbol;
if(pos2!=std::string::npos) clangmm::Cursor last_cursor;
data.emplace_back(usr.substr(pos1, pos2-pos1)); auto it=data.end();
do {
auto token_spelling=cursor.get_token_spelling();
if(!token_spelling.empty() && token_spelling!="__1") {
it=data.emplace(it, token_spelling);
if(symbol.empty())
symbol=token_spelling;
else else
data.emplace_back(usr.substr(pos1)); symbol.insert(0, token_spelling+"::");
} }
last_cursor=cursor;
cursor=cursor.get_semantic_parent();
} while(cursor.get_kind()!=clangmm::Cursor::Kind::TranslationUnit);
break; if(last_cursor.get_kind()!=clangmm::Cursor::Kind::Namespace)
} data.emplace(++data.begin(), "");
}
} auto url=Documentation::CppReference::get_url(symbol);
if(!url.empty())
return {url};
} }
if(data.empty()) if(data.empty())
Info::get().print("No symbol found at current cursor location"); Info::get().print("No symbol found at current cursor location");
return data; return data;
}; };

22
src/window.cc

@ -769,7 +769,10 @@ void Window::set_menu_actions() {
if(auto view=Notebook::get().get_current_view()) { if(auto view=Notebook::get().get_current_view()) {
if(view->get_token_data) { if(view->get_token_data) {
auto data=view->get_token_data(); auto data=view->get_token_data();
if(data.size()>0) { std::string url;
if(data.size()==1)
url=data[0];
else if(data.size()>1) {
auto documentation_search=Config::get().source.documentation_searches.find(data[0]); auto documentation_search=Config::get().source.documentation_searches.find(data[0]);
if(documentation_search!=Config::get().source.documentation_searches.end()) { if(documentation_search!=Config::get().source.documentation_searches.end()) {
std::string token_query; std::string token_query;
@ -789,25 +792,26 @@ void Window::set_menu_actions() {
if(query==documentation_search->second.queries.end()) if(query==documentation_search->second.queries.end())
query=documentation_search->second.queries.find("@any"); query=documentation_search->second.queries.find("@any");
if(query!=documentation_search->second.queries.end()) { if(query!=documentation_search->second.queries.end())
std::string uri=query->second+token_query; url=query->second+token_query;
}
}
}
if(!url.empty()) {
#ifdef __APPLE__ #ifdef __APPLE__
Terminal::get().process("open \""+uri+"\""); Terminal::get().process("open \""+url+"\"");
#else #else
GError* error=nullptr; GError* error=nullptr;
#if GTK_VERSION_GE(3, 22) #if GTK_VERSION_GE(3, 22)
gtk_show_uri_on_window(nullptr, uri.c_str(), GDK_CURRENT_TIME, &error); gtk_show_uri_on_window(nullptr, url.c_str(), GDK_CURRENT_TIME, &error);
#else #else
gtk_show_uri(nullptr, uri.c_str(), GDK_CURRENT_TIME, &error); gtk_show_uri(nullptr, url.c_str(), GDK_CURRENT_TIME, &error);
#endif #endif
g_clear_error(&error); g_clear_error(&error);
#endif #endif
} }
} }
} }
}
}
}
}); });
menu.add_action("source_goto_declaration", [this]() { menu.add_action("source_goto_declaration", [this]() {

Loading…
Cancel
Save