mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 01:12:44 +00:00 
			
		
		
		
	HackStudio: Highlight matching pairs of [ and ] as well
Also refactor the token pair matching code a little bit to not repeat ourselves so much. :^)
This commit is contained in:
		
							parent
							
								
									fc14bdd442
								
							
						
					
					
						commit
						8f9a522c37
					
				
					 2 changed files with 29 additions and 27 deletions
				
			
		|  | @ -172,7 +172,7 @@ void Editor::mousemove_event(GMouseEvent& event) | ||||||
|     GApplication::the().hide_tooltip(); |     GApplication::the().hide_tooltip(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void Editor::highlight_matching_curlies_or_parens() | void Editor::highlight_matching_token_pair() | ||||||
| { | { | ||||||
|     enum class Direction { |     enum class Direction { | ||||||
|         Forward, |         Forward, | ||||||
|  | @ -210,38 +210,40 @@ void Editor::highlight_matching_curlies_or_parens() | ||||||
|         update(); |         update(); | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|  |     struct MatchingTokenPair { | ||||||
|  |         CppToken::Type open; | ||||||
|  |         CppToken::Type close; | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|  |     MatchingTokenPair pairs[] = { | ||||||
|  |         { CppToken::Type::LeftCurly, CppToken::Type::RightCurly }, | ||||||
|  |         { CppToken::Type::LeftParen, CppToken::Type::RightParen }, | ||||||
|  |         { CppToken::Type::LeftBracket, CppToken::Type::RightBracket }, | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|     for (int i = 0; i < document().spans().size(); ++i) { |     for (int i = 0; i < document().spans().size(); ++i) { | ||||||
|         auto& span = const_cast<GTextDocumentSpan&>(document().spans().at(i)); |         auto& span = const_cast<GTextDocumentSpan&>(document().spans().at(i)); | ||||||
|         auto token_type = (CppToken::Type)((uintptr_t)span.data); |         auto token_type = (CppToken::Type)((uintptr_t)span.data); | ||||||
|         if (token_type == CppToken::Type::LeftCurly && span.range.start() == cursor()) { |  | ||||||
|             auto buddy = find_span_of_type(i, CppToken::Type::RightCurly, CppToken::Type::LeftCurly, Direction::Forward); |  | ||||||
|             if (buddy != -1) |  | ||||||
|                 make_buddies(i, buddy); |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
| 
 | 
 | ||||||
|         if (token_type == CppToken::Type::LeftParen && span.range.start() == cursor()) { |         for (auto& pair : pairs) { | ||||||
|             auto buddy = find_span_of_type(i, CppToken::Type::RightParen, CppToken::Type::LeftParen, Direction::Forward); |             if (token_type == pair.open && span.range.start() == cursor()) { | ||||||
|             if (buddy != -1) |                 auto buddy = find_span_of_type(i, pair.close, pair.open, Direction::Forward); | ||||||
|                 make_buddies(i, buddy); |                 if (buddy != -1) | ||||||
|             return; |                     make_buddies(i, buddy); | ||||||
|  |                 return; | ||||||
|  |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         auto right_of_end = span.range.end(); |         auto right_of_end = span.range.end(); | ||||||
|         right_of_end.set_column(right_of_end.column() + 1); |         right_of_end.set_column(right_of_end.column() + 1); | ||||||
| 
 | 
 | ||||||
|         if (token_type == CppToken::Type::RightCurly && right_of_end == cursor()) { |         for (auto& pair : pairs) { | ||||||
|             auto buddy = find_span_of_type(i, CppToken::Type::LeftCurly, CppToken::Type::RightCurly, Direction::Backward); |             if (token_type == pair.close && right_of_end == cursor()) { | ||||||
|             if (buddy != -1) |                 auto buddy = find_span_of_type(i, pair.open, pair.close, Direction::Backward); | ||||||
|                 make_buddies(i, buddy); |                 if (buddy != -1) | ||||||
|             return; |                     make_buddies(i, buddy); | ||||||
|         } |                 return; | ||||||
| 
 |             } | ||||||
|         if (token_type == CppToken::Type::RightParen && right_of_end == cursor()) { |  | ||||||
|             auto buddy = find_span_of_type(i, CppToken::Type::LeftParen, CppToken::Type::RightParen, Direction::Backward); |  | ||||||
|             if (buddy != -1) |  | ||||||
|                 make_buddies(i, buddy); |  | ||||||
|             return; |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | @ -256,11 +258,11 @@ void Editor::cursor_did_change() | ||||||
|         m_has_brace_buddies = false; |         m_has_brace_buddies = false; | ||||||
|         update(); |         update(); | ||||||
|     } |     } | ||||||
|     highlight_matching_curlies_or_parens(); |     highlight_matching_token_pair(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void Editor::notify_did_rehighlight() | void Editor::notify_did_rehighlight() | ||||||
| { | { | ||||||
|     m_has_brace_buddies = false; |     m_has_brace_buddies = false; | ||||||
|     highlight_matching_curlies_or_parens(); |     highlight_matching_token_pair(); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -25,7 +25,7 @@ private: | ||||||
|     virtual void cursor_did_change() override; |     virtual void cursor_did_change() override; | ||||||
| 
 | 
 | ||||||
|     void show_documentation_tooltip_if_available(const String&, const Point& screen_location); |     void show_documentation_tooltip_if_available(const String&, const Point& screen_location); | ||||||
|     void highlight_matching_curlies_or_parens(); |     void highlight_matching_token_pair(); | ||||||
| 
 | 
 | ||||||
|     explicit Editor(GWidget* parent); |     explicit Editor(GWidget* parent); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling