Sfoglia il codice sorgente

Add get_word_under_cursor() method to TextEdit

marynate 11 anni fa
parent
commit
212b8b2a03
2 ha cambiato i file con 24 aggiunte e 0 eliminazioni
  1. 22 0
      scene/gui/text_edit.cpp
  2. 2 0
      scene/gui/text_edit.h

+ 22 - 0
scene/gui/text_edit.cpp

@@ -2485,6 +2485,27 @@ String TextEdit::get_selection_text() const {
 
 }
 
+String TextEdit::get_word_under_cursor() const {
+
+	int prev_cc = cursor.column;
+	while(prev_cc >0) {
+		bool is_char = _is_text_char(text[cursor.line][prev_cc-1]);
+		if (!is_char)
+			break;
+		--prev_cc;
+	}
+
+	int next_cc = cursor.column;
+	while(next_cc<text[cursor.line].length()) {
+		bool is_char = _is_text_char(text[cursor.line][next_cc]);
+		if(!is_char)
+			break;
+		++ next_cc;
+	}
+	if (prev_cc == cursor.column || next_cc == cursor.column)
+		return "";
+	return text[cursor.line].substr(prev_cc, next_cc-prev_cc);
+}
 
 DVector<int> TextEdit::_search_bind(const String &p_key,uint32_t p_search_flags, int p_from_line,int p_from_column) const {
 
@@ -3037,6 +3058,7 @@ void TextEdit::_bind_methods() {
 	ObjectTypeDB::bind_method(_MD("get_selection_to_line"),&TextEdit::get_selection_to_line);
 	ObjectTypeDB::bind_method(_MD("get_selection_to_column"),&TextEdit::get_selection_to_column);
 	ObjectTypeDB::bind_method(_MD("get_selection_text"),&TextEdit::get_selection_text);
+	ObjectTypeDB::bind_method(_MD("get_word_under_cursor"),&TextEdit::get_word_under_cursor);
 	ObjectTypeDB::bind_method(_MD("search","flags","from_line","from_column","to_line","to_column"),&TextEdit::_search_bind);
 
 	ObjectTypeDB::bind_method(_MD("undo"),&TextEdit::undo);

+ 2 - 0
scene/gui/text_edit.h

@@ -341,6 +341,8 @@ public:
 	int get_selection_to_column() const;
 	String get_selection_text() const;
 
+	String get_word_under_cursor() const;
+
 	bool search(const String &p_key,uint32_t p_search_flags, int p_from_line, int p_from_column,int &r_line,int &r_column) const;
 
 	void undo();