Browse Source

Source token cleanup. Rename refactor should now work even better.

merge-requests/365/head
eidheim 10 years ago
parent
commit
74434779c3
  1. 36
      src/source.cc
  2. 16
      src/source.h
  3. 11
      src/window.cc

36
src/source.cc

@ -1943,7 +1943,7 @@ Source::ClangViewAutocomplete(file_path, project_path, language) {
}
});
get_token=[this]() -> std::pair<std::string, int> {
get_token=[this]() -> Token {
if(source_readable) {
auto iter=get_buffer()->get_insert()->get_iter();
auto line=(unsigned)iter.get_line();
@ -1956,51 +1956,35 @@ Source::ClangViewAutocomplete(file_path, project_path, language) {
continue;
auto referenced=cursor.get_referenced();
if(referenced)
return {referenced.get_usr(), static_cast<int>(referenced.get_kind())};
return Token(static_cast<int>(referenced.get_kind()), token.get_spelling(), referenced.get_usr());
}
}
}
}
return {"", -1};
return Token(-1, "", "");
};
get_token_name=[this]() -> std::string {
tag_similar_tokens=[this](const Token &token){
if(source_readable) {
auto iter=get_buffer()->get_insert()->get_iter();
auto line=(unsigned)iter.get_line();
auto index=(unsigned)iter.get_line_index();
for(auto &token: *clang_tokens) {
if(token.get_kind()==clang::Token_Identifier && token.get_cursor().has_type()) {
if(line==token.offsets.first.line-1 && index>=token.offsets.first.index-1 && index <=token.offsets.second.index-1) {
return token.get_spelling();
}
}
}
}
return "";
};
tag_similar_tokens=[this](const std::pair<std::string, int> &token){
if(source_readable) {
if(token.second>=0 && token.first.size()>0 && last_similar_tokens_tagged!=token.first) {
if(token.type>=0 && token.usr.size()>0 && last_similar_tokens_tagged!=token.usr) {
get_buffer()->remove_tag(similar_tokens_tag, get_buffer()->begin(), get_buffer()->end());
auto offsets=clang_tokens->get_similar_token_offsets(token.first, static_cast<clang::CursorKind>(token.second));
auto offsets=clang_tokens->get_similar_token_offsets(static_cast<clang::CursorKind>(token.type), token.spelling, token.usr);
for(auto &offset: offsets) {
get_buffer()->apply_tag(similar_tokens_tag, get_buffer()->get_iter_at_line_index(offset.first.line-1, offset.first.index-1), get_buffer()->get_iter_at_line_index(offset.second.line-1, offset.second.index-1));
}
last_similar_tokens_tagged=token.first;
last_similar_tokens_tagged=token.usr;
}
}
if(token.second<0 && token.first.size()==0 && last_similar_tokens_tagged!="") {
if(token.type<0 && token.usr.size()==0 && last_similar_tokens_tagged!="") {
get_buffer()->remove_tag(similar_tokens_tag, get_buffer()->begin(), get_buffer()->end());
last_similar_tokens_tagged="";
}
};
rename_similar_tokens=[this](const std::pair<std::string, int> &token, const std::string &text) {
rename_similar_tokens=[this](const Token &token, const std::string &text) {
size_t number=0;
if(source_readable) {
auto offsets=clang_tokens->get_similar_token_offsets(token.first, static_cast<clang::CursorKind>(token.second));
auto offsets=clang_tokens->get_similar_token_offsets(static_cast<clang::CursorKind>(token.type), token.spelling, token.usr);
std::vector<std::pair<Glib::RefPtr<Gtk::TextMark>, Glib::RefPtr<Gtk::TextMark> > > marks;
for(auto &offset: offsets) {
marks.emplace_back(get_buffer()->create_mark(get_buffer()->get_iter_at_line_index(offset.first.line-1, offset.first.index-1)), get_buffer()->create_mark(get_buffer()->get_iter_at_line_index(offset.second.line-1, offset.second.index-1)));

16
src/source.h

@ -49,6 +49,15 @@ namespace Source {
std::vector<clang::CompletionChunk> chunks;
std::string brief_comments;
};
class Token {
public:
Token(int type, const std::string &spelling, const std::string &usr):
type(type), spelling(spelling), usr(usr) {}
int type;
std::string spelling;
std::string usr;
};
class View : public Gsv::View {
public:
@ -71,10 +80,9 @@ namespace Source {
std::function<std::pair<std::string, clang::Offset>()> get_declaration_location;
std::function<void()> goto_method;
std::function<std::pair<std::string, int>()> get_token;
std::function<std::string()> get_token_name;
std::function<void(const std::pair<std::string, int> &token)> tag_similar_tokens;
std::function<size_t(const std::pair<std::string, int> &token, const std::string &text)> rename_similar_tokens;
std::function<Token()> get_token;
std::function<void(const Token &token)> tag_similar_tokens;
std::function<size_t(const Token &token, const std::string &text)> rename_similar_tokens;
std::function<void()> goto_next_diagnostic;
std::function<void(View* view, const std::string &status)> on_update_status;

11
src/window.cc

@ -722,10 +722,9 @@ void Window::goto_line_entry() {
void Window::rename_token_entry() {
entry_box.clear();
if(notebook.get_current_page()!=-1) {
if(notebook.get_current_view()->get_token && notebook.get_current_view()->get_token_name) {
auto token=std::make_shared<std::pair<std::string, int> >(notebook.get_current_view()->get_token());
if(token->second>=0 && token->first.size()>0 && notebook.get_current_view()->get_token_name) {
auto token_name=std::make_shared<std::string>(notebook.get_current_view()->get_token_name());
if(notebook.get_current_view()->get_token) {
auto token=std::make_shared<Source::Token>(notebook.get_current_view()->get_token());
if(token->type>=0 && token->usr.size()>0) {
for(int c=0;c<notebook.size();c++) {
if(notebook.get_view(c)->tag_similar_tokens) {
notebook.get_view(c)->tag_similar_tokens(*token);
@ -737,8 +736,8 @@ void Window::rename_token_entry() {
label_it->set_text("Warning: only opened and parsed tabs will have its content renamed, and modified files will be saved.");
};
label_it->update(0, "");
entry_box.entries.emplace_back(*token_name, [this, token_name, token](const std::string& content){
if(notebook.get_current_page()!=-1 && content!=*token_name) {
entry_box.entries.emplace_back(token->spelling, [this, token](const std::string& content){
if(notebook.get_current_page()!=-1 && content!=token->spelling) {
for(int c=0;c<notebook.size();c++) {
auto view=notebook.get_view(c);
if(view->rename_similar_tokens) {

Loading…
Cancel
Save