mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 09:02:43 +00:00 
			
		
		
		
	LibGfx: Add Gfx::TextAttributes (and use it in GUI::TextDocumentSpan)
This commit is contained in:
		
							parent
							
								
									0d44ee6f2b
								
							
						
					
					
						commit
						05f5d0dda3
					
				
					 11 changed files with 127 additions and 83 deletions
				
			
		|  | @ -47,23 +47,29 @@ void CellSyntaxHighlighter::rehighlight(Gfx::Palette palette) | ||||||
|     // Highlight the '='
 |     // Highlight the '='
 | ||||||
|     m_editor->document().spans().empend( |     m_editor->document().spans().empend( | ||||||
|         GUI::TextRange { { 0, 0 }, { 0, 1 } }, |         GUI::TextRange { { 0, 0 }, { 0, 1 } }, | ||||||
|  |         Gfx::TextAttributes { | ||||||
|             palette.syntax_keyword(), |             palette.syntax_keyword(), | ||||||
|             Optional<Color> {}, |             Optional<Color> {}, | ||||||
|             false, |             false, | ||||||
|             false, |             false, | ||||||
|         false, |         }, | ||||||
|         nullptr); |         nullptr, | ||||||
|  |         false); | ||||||
| 
 | 
 | ||||||
|     if (m_cell && m_cell->exception()) { |     if (m_cell && m_cell->exception()) { | ||||||
|         auto range = m_cell->exception()->source_ranges().first(); |         auto range = m_cell->exception()->source_ranges().first(); | ||||||
|         GUI::TextRange text_range { { range.start.line - 1, range.start.column }, { range.end.line - 1, range.end.column - 1 } }; |         GUI::TextRange text_range { { range.start.line - 1, range.start.column }, { range.end.line - 1, range.end.column - 1 } }; | ||||||
|         m_editor->document().spans().prepend({ text_range, |         m_editor->document().spans().prepend( | ||||||
|  |             GUI::TextDocumentSpan { | ||||||
|  |                 text_range, | ||||||
|  |                 Gfx::TextAttributes { | ||||||
|                     Color::Black, |                     Color::Black, | ||||||
|                     Color::Red, |                     Color::Red, | ||||||
|                     false, |                     false, | ||||||
|                     false, |                     false, | ||||||
|             false, |                 }, | ||||||
|             nullptr }); |                 nullptr, | ||||||
|  |                 false }); | ||||||
|     } |     } | ||||||
|     m_editor->update(); |     m_editor->update(); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -226,8 +226,8 @@ void Editor::mousemove_event(GUI::MouseEvent& event) | ||||||
| 
 | 
 | ||||||
|     for (auto& span : document().spans()) { |     for (auto& span : document().spans()) { | ||||||
|         if (span.range.contains(m_previous_text_position) && !span.range.contains(text_position)) { |         if (span.range.contains(m_previous_text_position) && !span.range.contains(text_position)) { | ||||||
|             if (highlighter->is_navigatable(span.data) && span.is_underlined) { |             if (highlighter->is_navigatable(span.data) && span.attributes.underline) { | ||||||
|                 span.is_underlined = false; |                 span.attributes.underline = false; | ||||||
|                 wrapper().editor().update(); |                 wrapper().editor().update(); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  | @ -243,9 +243,9 @@ void Editor::mousemove_event(GUI::MouseEvent& event) | ||||||
| 
 | 
 | ||||||
|             if (highlighter->is_navigatable(span.data)) { |             if (highlighter->is_navigatable(span.data)) { | ||||||
|                 is_over_link = true; |                 is_over_link = true; | ||||||
|                 bool was_underlined = span.is_underlined; |                 bool was_underlined = span.attributes.underline; | ||||||
|                 span.is_underlined = event.modifiers() & Mod_Ctrl; |                 span.attributes.underline = event.modifiers() & Mod_Ctrl; | ||||||
|                 if (span.is_underlined != was_underlined) { |                 if (span.attributes.underline != was_underlined) { | ||||||
|                     wrapper().editor().update(); |                     wrapper().editor().update(); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |  | ||||||
|  | @ -90,8 +90,8 @@ void CppSyntaxHighlighter::rehighlight(Gfx::Palette palette) | ||||||
|         span.range.set_start({ token.m_start.line, token.m_start.column }); |         span.range.set_start({ token.m_start.line, token.m_start.column }); | ||||||
|         span.range.set_end({ token.m_end.line, token.m_end.column }); |         span.range.set_end({ token.m_end.line, token.m_end.column }); | ||||||
|         auto style = style_for_token_type(palette, token.m_type); |         auto style = style_for_token_type(palette, token.m_type); | ||||||
|         span.color = style.color; |         span.attributes.color = style.color; | ||||||
|         span.bold = style.bold; |         span.attributes.bold = style.bold; | ||||||
|         span.is_skippable = token.m_type == Cpp::Token::Type::Whitespace; |         span.is_skippable = token.m_type == Cpp::Token::Type::Whitespace; | ||||||
|         span.data = reinterpret_cast<void*>(token.m_type); |         span.data = reinterpret_cast<void*>(token.m_type); | ||||||
|         spans.append(span); |         spans.append(span); | ||||||
|  |  | ||||||
|  | @ -72,8 +72,8 @@ void GMLSyntaxHighlighter::rehighlight(Gfx::Palette palette) | ||||||
|         span.range.set_start({ token.m_start.line, token.m_start.column }); |         span.range.set_start({ token.m_start.line, token.m_start.column }); | ||||||
|         span.range.set_end({ token.m_end.line, token.m_end.column }); |         span.range.set_end({ token.m_end.line, token.m_end.column }); | ||||||
|         auto style = style_for_token_type(palette, token.m_type); |         auto style = style_for_token_type(palette, token.m_type); | ||||||
|         span.color = style.color; |         span.attributes.color = style.color; | ||||||
|         span.bold = style.bold; |         span.attributes.bold = style.bold; | ||||||
|         span.is_skippable = false; |         span.is_skippable = false; | ||||||
|         span.data = reinterpret_cast<void*>(token.m_type); |         span.data = reinterpret_cast<void*>(token.m_type); | ||||||
|         spans.append(span); |         spans.append(span); | ||||||
|  |  | ||||||
|  | @ -71,8 +71,8 @@ void IniSyntaxHighlighter::rehighlight(Gfx::Palette palette) | ||||||
|         span.range.set_start({ token.m_start.line, token.m_start.column }); |         span.range.set_start({ token.m_start.line, token.m_start.column }); | ||||||
|         span.range.set_end({ token.m_end.line, token.m_end.column }); |         span.range.set_end({ token.m_end.line, token.m_end.column }); | ||||||
|         auto style = style_for_token_type(palette, token.m_type); |         auto style = style_for_token_type(palette, token.m_type); | ||||||
|         span.color = style.color; |         span.attributes.color = style.color; | ||||||
|         span.bold = style.bold; |         span.attributes.bold = style.bold; | ||||||
|         span.is_skippable = token.m_type == IniToken::Type::Whitespace; |         span.is_skippable = token.m_type == IniToken::Type::Whitespace; | ||||||
|         span.data = reinterpret_cast<void*>(token.m_type); |         span.data = reinterpret_cast<void*>(token.m_type); | ||||||
|         spans.append(span); |         spans.append(span); | ||||||
|  |  | ||||||
|  | @ -100,8 +100,8 @@ void JSSyntaxHighlighter::rehighlight(Gfx::Palette palette) | ||||||
|         span.range.set_end({ position.line(), position.column() }); |         span.range.set_end({ position.line(), position.column() }); | ||||||
|         auto type = is_trivia ? JS::TokenType::Invalid : token.type(); |         auto type = is_trivia ? JS::TokenType::Invalid : token.type(); | ||||||
|         auto style = style_for_token_type(palette, type); |         auto style = style_for_token_type(palette, type); | ||||||
|         span.color = style.color; |         span.attributes.color = style.color; | ||||||
|         span.bold = style.bold; |         span.attributes.bold = style.bold; | ||||||
|         span.is_skippable = is_trivia; |         span.is_skippable = is_trivia; | ||||||
|         span.data = reinterpret_cast<void*>(static_cast<size_t>(type)); |         span.data = reinterpret_cast<void*>(static_cast<size_t>(type)); | ||||||
|         spans.append(span); |         spans.append(span); | ||||||
|  |  | ||||||
|  | @ -104,8 +104,8 @@ private: | ||||||
|     { |     { | ||||||
|         if (node->path()->is_bareword()) { |         if (node->path()->is_bareword()) { | ||||||
|             auto& span = span_for_node(node->path()); |             auto& span = span_for_node(node->path()); | ||||||
|             span.color = m_palette.link(); |             span.attributes.color = m_palette.link(); | ||||||
|             span.is_underlined = true; |             span.attributes.underline = true; | ||||||
|         } else { |         } else { | ||||||
|             NodeVisitor::visit(node); |             NodeVisitor::visit(node); | ||||||
|         } |         } | ||||||
|  | @ -124,8 +124,8 @@ private: | ||||||
|         auto& span = span_for_node(node); |         auto& span = span_for_node(node); | ||||||
|         span.range.set_start({ node->and_position().start_line.line_number, node->and_position().start_line.line_column }); |         span.range.set_start({ node->and_position().start_line.line_number, node->and_position().start_line.line_column }); | ||||||
|         set_offset_range_end(span.range, node->and_position().end_line); |         set_offset_range_end(span.range, node->and_position().end_line); | ||||||
|         span.color = m_palette.syntax_punctuation(); |         span.attributes.color = m_palette.syntax_punctuation(); | ||||||
|         span.bold = true; |         span.attributes.bold = true; | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::ListConcatenate* node) override |     virtual void visit(const AST::ListConcatenate* node) override | ||||||
|     { |     { | ||||||
|  | @ -137,8 +137,8 @@ private: | ||||||
| 
 | 
 | ||||||
|         auto& span = span_for_node(node); |         auto& span = span_for_node(node); | ||||||
|         set_offset_range_start(span.range, node->position().end_line); |         set_offset_range_start(span.range, node->position().end_line); | ||||||
|         span.color = m_palette.syntax_punctuation(); |         span.attributes.color = m_palette.syntax_punctuation(); | ||||||
|         span.bold = true; |         span.attributes.bold = true; | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::BraceExpansion* node) override |     virtual void visit(const AST::BraceExpansion* node) override | ||||||
|     { |     { | ||||||
|  | @ -150,11 +150,11 @@ private: | ||||||
| 
 | 
 | ||||||
|         auto& span = span_for_node(node); |         auto& span = span_for_node(node); | ||||||
|         if (m_is_first_in_command) { |         if (m_is_first_in_command) { | ||||||
|             span.color = m_palette.syntax_keyword(); |             span.attributes.color = m_palette.syntax_keyword(); | ||||||
|             span.bold = true; |             span.attributes.bold = true; | ||||||
|             m_is_first_in_command = false; |             m_is_first_in_command = false; | ||||||
|         } else if (node->text().starts_with("-")) { |         } else if (node->text().starts_with("-")) { | ||||||
|             span.color = m_palette.syntax_preprocessor_statement(); |             span.attributes.color = m_palette.syntax_preprocessor_statement(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::CastToCommand* node) override |     virtual void visit(const AST::CastToCommand* node) override | ||||||
|  | @ -166,12 +166,12 @@ private: | ||||||
|         NodeVisitor::visit(node); |         NodeVisitor::visit(node); | ||||||
| 
 | 
 | ||||||
|         auto& start_span = span_for_node(node); |         auto& start_span = span_for_node(node); | ||||||
|         start_span.color = m_palette.syntax_punctuation(); |         start_span.attributes.color = m_palette.syntax_punctuation(); | ||||||
|         start_span.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column + 1 }); |         start_span.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column + 1 }); | ||||||
|         start_span.data = (void*)static_cast<size_t>(AugmentedTokenKind::OpenParen); |         start_span.data = (void*)static_cast<size_t>(AugmentedTokenKind::OpenParen); | ||||||
| 
 | 
 | ||||||
|         auto& end_span = span_for_node(node); |         auto& end_span = span_for_node(node); | ||||||
|         end_span.color = m_palette.syntax_punctuation(); |         end_span.attributes.color = m_palette.syntax_punctuation(); | ||||||
|         set_offset_range_start(end_span.range, node->position().end_line); |         set_offset_range_start(end_span.range, node->position().end_line); | ||||||
|         end_span.data = (void*)static_cast<size_t>(AugmentedTokenKind::CloseParen); |         end_span.data = (void*)static_cast<size_t>(AugmentedTokenKind::CloseParen); | ||||||
|     } |     } | ||||||
|  | @ -188,21 +188,21 @@ private: | ||||||
|         NodeVisitor::visit(node); |         NodeVisitor::visit(node); | ||||||
| 
 | 
 | ||||||
|         auto& span = span_for_node(node); |         auto& span = span_for_node(node); | ||||||
|         span.color = m_palette.syntax_comment(); |         span.attributes.color = m_palette.syntax_comment(); | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::ContinuationControl* node) override |     virtual void visit(const AST::ContinuationControl* node) override | ||||||
|     { |     { | ||||||
|         NodeVisitor::visit(node); |         NodeVisitor::visit(node); | ||||||
| 
 | 
 | ||||||
|         auto& span = span_for_node(node); |         auto& span = span_for_node(node); | ||||||
|         span.color = m_palette.syntax_control_keyword(); |         span.attributes.color = m_palette.syntax_control_keyword(); | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::DynamicEvaluate* node) override |     virtual void visit(const AST::DynamicEvaluate* node) override | ||||||
|     { |     { | ||||||
|         NodeVisitor::visit(node); |         NodeVisitor::visit(node); | ||||||
| 
 | 
 | ||||||
|         auto& start_span = span_for_node(node); |         auto& start_span = span_for_node(node); | ||||||
|         start_span.color = m_palette.syntax_punctuation(); |         start_span.attributes.color = m_palette.syntax_punctuation(); | ||||||
|         start_span.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column }); |         start_span.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column }); | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::DoubleQuotedString* node) override |     virtual void visit(const AST::DoubleQuotedString* node) override | ||||||
|  | @ -210,18 +210,18 @@ private: | ||||||
|         NodeVisitor::visit(node); |         NodeVisitor::visit(node); | ||||||
| 
 | 
 | ||||||
|         auto& start_span = span_for_node(node); |         auto& start_span = span_for_node(node); | ||||||
|         start_span.color = m_palette.syntax_string(); |         start_span.attributes.color = m_palette.syntax_string(); | ||||||
|         set_offset_range_end(start_span.range, node->position().start_line, 0); |         set_offset_range_end(start_span.range, node->position().start_line, 0); | ||||||
|         start_span.is_skippable = true; |         start_span.is_skippable = true; | ||||||
| 
 | 
 | ||||||
|         auto& end_span = span_for_node(node); |         auto& end_span = span_for_node(node); | ||||||
|         set_offset_range_start(end_span.range, node->position().end_line); |         set_offset_range_start(end_span.range, node->position().end_line); | ||||||
|         end_span.color = m_palette.syntax_string(); |         end_span.attributes.color = m_palette.syntax_string(); | ||||||
|         end_span.is_skippable = true; |         end_span.is_skippable = true; | ||||||
| 
 | 
 | ||||||
|         if (m_is_first_in_command) { |         if (m_is_first_in_command) { | ||||||
|             start_span.bold = true; |             start_span.attributes.bold = true; | ||||||
|             end_span.bold = true; |             end_span.attributes.bold = true; | ||||||
|         } |         } | ||||||
|         m_is_first_in_command = false; |         m_is_first_in_command = false; | ||||||
|     } |     } | ||||||
|  | @ -237,14 +237,14 @@ private: | ||||||
|         auto& name_span = span_for_node(node); |         auto& name_span = span_for_node(node); | ||||||
|         name_span.range.set_start({ node->name().position.start_line.line_number, node->name().position.start_line.line_column }); |         name_span.range.set_start({ node->name().position.start_line.line_number, node->name().position.start_line.line_column }); | ||||||
|         set_offset_range_end(name_span.range, node->name().position.end_line); |         set_offset_range_end(name_span.range, node->name().position.end_line); | ||||||
|         name_span.color = m_palette.syntax_identifier(); |         name_span.attributes.color = m_palette.syntax_identifier(); | ||||||
| 
 | 
 | ||||||
|         // arguments
 |         // arguments
 | ||||||
|         for (auto& arg : node->arguments()) { |         for (auto& arg : node->arguments()) { | ||||||
|             auto& name_span = span_for_node(node); |             auto& name_span = span_for_node(node); | ||||||
|             name_span.range.set_start({ arg.position.start_line.line_number, arg.position.start_line.line_column }); |             name_span.range.set_start({ arg.position.start_line.line_number, arg.position.start_line.line_column }); | ||||||
|             set_offset_range_end(name_span.range, arg.position.end_line); |             set_offset_range_end(name_span.range, arg.position.end_line); | ||||||
|             name_span.color = m_palette.syntax_identifier(); |             name_span.attributes.color = m_palette.syntax_identifier(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::ForLoop* node) override |     virtual void visit(const AST::ForLoop* node) override | ||||||
|  | @ -257,7 +257,7 @@ private: | ||||||
|         auto& for_span = span_for_node(node); |         auto& for_span = span_for_node(node); | ||||||
|         // FIXME: "fo\\\nr" is valid too
 |         // FIXME: "fo\\\nr" is valid too
 | ||||||
|         for_span.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column + 2 }); |         for_span.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column + 2 }); | ||||||
|         for_span.color = m_palette.syntax_keyword(); |         for_span.attributes.color = m_palette.syntax_keyword(); | ||||||
| 
 | 
 | ||||||
|         // "in"
 |         // "in"
 | ||||||
|         if (auto maybe_position = node->in_keyword_position(); maybe_position.has_value()) { |         if (auto maybe_position = node->in_keyword_position(); maybe_position.has_value()) { | ||||||
|  | @ -266,7 +266,7 @@ private: | ||||||
|             auto& in_span = span_for_node(node); |             auto& in_span = span_for_node(node); | ||||||
|             in_span.range.set_start({ position.start_line.line_number, position.start_line.line_column }); |             in_span.range.set_start({ position.start_line.line_number, position.start_line.line_column }); | ||||||
|             set_offset_range_end(in_span.range, position.end_line); |             set_offset_range_end(in_span.range, position.end_line); | ||||||
|             in_span.color = m_palette.syntax_keyword(); |             in_span.attributes.color = m_palette.syntax_keyword(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::Glob* node) override |     virtual void visit(const AST::Glob* node) override | ||||||
|  | @ -274,7 +274,7 @@ private: | ||||||
|         NodeVisitor::visit(node); |         NodeVisitor::visit(node); | ||||||
| 
 | 
 | ||||||
|         auto& span = span_for_node(node); |         auto& span = span_for_node(node); | ||||||
|         span.color = m_palette.syntax_preprocessor_value(); |         span.attributes.color = m_palette.syntax_preprocessor_value(); | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::Execute* node) override |     virtual void visit(const AST::Execute* node) override | ||||||
|     { |     { | ||||||
|  | @ -283,12 +283,12 @@ private: | ||||||
| 
 | 
 | ||||||
|         if (node->does_capture_stdout()) { |         if (node->does_capture_stdout()) { | ||||||
|             auto& start_span = span_for_node(node); |             auto& start_span = span_for_node(node); | ||||||
|             start_span.color = m_palette.syntax_punctuation(); |             start_span.attributes.color = m_palette.syntax_punctuation(); | ||||||
|             start_span.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column + 1 }); |             start_span.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column + 1 }); | ||||||
|             start_span.data = (void*)static_cast<size_t>(AugmentedTokenKind::OpenParen); |             start_span.data = (void*)static_cast<size_t>(AugmentedTokenKind::OpenParen); | ||||||
| 
 | 
 | ||||||
|             auto& end_span = span_for_node(node); |             auto& end_span = span_for_node(node); | ||||||
|             end_span.color = m_palette.syntax_punctuation(); |             end_span.attributes.color = m_palette.syntax_punctuation(); | ||||||
|             set_offset_range_start(end_span.range, node->position().end_line); |             set_offset_range_start(end_span.range, node->position().end_line); | ||||||
|             end_span.data = (void*)static_cast<size_t>(AugmentedTokenKind::CloseParen); |             end_span.data = (void*)static_cast<size_t>(AugmentedTokenKind::CloseParen); | ||||||
|         } |         } | ||||||
|  | @ -302,7 +302,7 @@ private: | ||||||
|         auto& if_span = span_for_node(node); |         auto& if_span = span_for_node(node); | ||||||
|         // FIXME: "i\\\nf" is valid too
 |         // FIXME: "i\\\nf" is valid too
 | ||||||
|         if_span.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column + 1 }); |         if_span.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column + 1 }); | ||||||
|         if_span.color = m_palette.syntax_keyword(); |         if_span.attributes.color = m_palette.syntax_keyword(); | ||||||
| 
 | 
 | ||||||
|         // "else"
 |         // "else"
 | ||||||
|         if (auto maybe_position = node->else_position(); maybe_position.has_value()) { |         if (auto maybe_position = node->else_position(); maybe_position.has_value()) { | ||||||
|  | @ -311,7 +311,7 @@ private: | ||||||
|             auto& else_span = span_for_node(node); |             auto& else_span = span_for_node(node); | ||||||
|             else_span.range.set_start({ position.start_line.line_number, position.start_line.line_column }); |             else_span.range.set_start({ position.start_line.line_number, position.start_line.line_column }); | ||||||
|             set_offset_range_end(else_span.range, node->position().end_line); |             set_offset_range_end(else_span.range, node->position().end_line); | ||||||
|             else_span.color = m_palette.syntax_keyword(); |             else_span.attributes.color = m_palette.syntax_keyword(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::Join* node) override |     virtual void visit(const AST::Join* node) override | ||||||
|  | @ -328,7 +328,7 @@ private: | ||||||
|         auto& match_expr = span_for_node(node); |         auto& match_expr = span_for_node(node); | ||||||
|         // FIXME: "mat\\\nch" is valid too
 |         // FIXME: "mat\\\nch" is valid too
 | ||||||
|         match_expr.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column + 4 }); |         match_expr.range.set_end({ node->position().start_line.line_number, node->position().start_line.line_column + 4 }); | ||||||
|         match_expr.color = m_palette.syntax_keyword(); |         match_expr.attributes.color = m_palette.syntax_keyword(); | ||||||
| 
 | 
 | ||||||
|         // "as"
 |         // "as"
 | ||||||
|         if (auto maybe_position = node->as_position(); maybe_position.has_value()) { |         if (auto maybe_position = node->as_position(); maybe_position.has_value()) { | ||||||
|  | @ -337,7 +337,7 @@ private: | ||||||
|             auto& as_span = span_for_node(node); |             auto& as_span = span_for_node(node); | ||||||
|             as_span.range.set_start({ position.start_line.line_number, position.start_line.line_column }); |             as_span.range.set_start({ position.start_line.line_number, position.start_line.line_column }); | ||||||
|             as_span.range.set_end({ position.end_line.line_number, position.end_line.line_column }); |             as_span.range.set_end({ position.end_line.line_number, position.end_line.line_column }); | ||||||
|             as_span.color = m_palette.syntax_keyword(); |             as_span.attributes.color = m_palette.syntax_keyword(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::Or* node) override |     virtual void visit(const AST::Or* node) override | ||||||
|  | @ -354,8 +354,8 @@ private: | ||||||
|         auto& span = span_for_node(node); |         auto& span = span_for_node(node); | ||||||
|         span.range.set_start({ node->or_position().start_line.line_number, node->or_position().start_line.line_column }); |         span.range.set_start({ node->or_position().start_line.line_number, node->or_position().start_line.line_column }); | ||||||
|         set_offset_range_end(span.range, node->or_position().end_line); |         set_offset_range_end(span.range, node->or_position().end_line); | ||||||
|         span.color = m_palette.syntax_punctuation(); |         span.attributes.color = m_palette.syntax_punctuation(); | ||||||
|         span.bold = true; |         span.attributes.bold = true; | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::Pipe* node) override |     virtual void visit(const AST::Pipe* node) override | ||||||
|     { |     { | ||||||
|  | @ -368,7 +368,7 @@ private: | ||||||
|         auto& span = span_for_node(node->start()); |         auto& span = span_for_node(node->start()); | ||||||
|         span.range.set_start(span.range.end()); |         span.range.set_start(span.range.end()); | ||||||
|         set_offset_range_end(span.range, node->start()->position().end_line, 2); |         set_offset_range_end(span.range, node->start()->position().end_line, 2); | ||||||
|         span.color = m_palette.syntax_punctuation(); |         span.attributes.color = m_palette.syntax_punctuation(); | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::ReadRedirection* node) override |     virtual void visit(const AST::ReadRedirection* node) override | ||||||
|     { |     { | ||||||
|  | @ -392,8 +392,8 @@ private: | ||||||
|         auto& span = span_for_node(node); |         auto& span = span_for_node(node); | ||||||
|         span.range.set_start({ node->separator_position().start_line.line_number, node->separator_position().start_line.line_column }); |         span.range.set_start({ node->separator_position().start_line.line_number, node->separator_position().start_line.line_column }); | ||||||
|         set_offset_range_end(span.range, node->separator_position().end_line); |         set_offset_range_end(span.range, node->separator_position().end_line); | ||||||
|         span.color = m_palette.syntax_punctuation(); |         span.attributes.color = m_palette.syntax_punctuation(); | ||||||
|         span.bold = true; |         span.attributes.bold = true; | ||||||
|         span.is_skippable = true; |         span.is_skippable = true; | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::Subshell* node) override |     virtual void visit(const AST::Subshell* node) override | ||||||
|  | @ -405,14 +405,14 @@ private: | ||||||
|         NodeVisitor::visit(node); |         NodeVisitor::visit(node); | ||||||
| 
 | 
 | ||||||
|         auto& span = span_for_node(node); |         auto& span = span_for_node(node); | ||||||
|         span.color = m_palette.syntax_identifier(); |         span.attributes.color = m_palette.syntax_identifier(); | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::SpecialVariable* node) override |     virtual void visit(const AST::SpecialVariable* node) override | ||||||
|     { |     { | ||||||
|         NodeVisitor::visit(node); |         NodeVisitor::visit(node); | ||||||
| 
 | 
 | ||||||
|         auto& span = span_for_node(node); |         auto& span = span_for_node(node); | ||||||
|         span.color = m_palette.syntax_identifier(); |         span.attributes.color = m_palette.syntax_identifier(); | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::Juxtaposition* node) override |     virtual void visit(const AST::Juxtaposition* node) override | ||||||
|     { |     { | ||||||
|  | @ -426,9 +426,9 @@ private: | ||||||
|             return; |             return; | ||||||
| 
 | 
 | ||||||
|         auto& span = span_for_node(node); |         auto& span = span_for_node(node); | ||||||
|         span.color = m_palette.syntax_string(); |         span.attributes.color = m_palette.syntax_string(); | ||||||
|         if (m_is_first_in_command) |         if (m_is_first_in_command) | ||||||
|             span.bold = true; |             span.attributes.bold = true; | ||||||
|         m_is_first_in_command = false; |         m_is_first_in_command = false; | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::StringPartCompose* node) override |     virtual void visit(const AST::StringPartCompose* node) override | ||||||
|  | @ -440,22 +440,22 @@ private: | ||||||
|         NodeVisitor::visit(node); |         NodeVisitor::visit(node); | ||||||
| 
 | 
 | ||||||
|         auto& span = span_for_node(node); |         auto& span = span_for_node(node); | ||||||
|         span.is_underlined = true; |         span.attributes.underline = true; | ||||||
|         span.background_color = Color(Color::NamedColor::MidRed).lightened(1.3f).with_alpha(128); |         span.attributes.background_color = Color(Color::NamedColor::MidRed).lightened(1.3f).with_alpha(128); | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::Tilde* node) override |     virtual void visit(const AST::Tilde* node) override | ||||||
|     { |     { | ||||||
|         NodeVisitor::visit(node); |         NodeVisitor::visit(node); | ||||||
| 
 | 
 | ||||||
|         auto& span = span_for_node(node); |         auto& span = span_for_node(node); | ||||||
|         span.color = m_palette.link(); |         span.attributes.color = m_palette.link(); | ||||||
|     } |     } | ||||||
|     virtual void visit(const AST::VariableDeclarations* node) override |     virtual void visit(const AST::VariableDeclarations* node) override | ||||||
|     { |     { | ||||||
|         TemporaryChange first_in_command { m_is_first_in_command, false }; |         TemporaryChange first_in_command { m_is_first_in_command, false }; | ||||||
|         for (auto& decl : node->variables()) { |         for (auto& decl : node->variables()) { | ||||||
|             auto& name_span = span_for_node(decl.name); |             auto& name_span = span_for_node(decl.name); | ||||||
|             name_span.color = m_palette.syntax_identifier(); |             name_span.attributes.color = m_palette.syntax_identifier(); | ||||||
| 
 | 
 | ||||||
|             decl.name->visit(*this); |             decl.name->visit(*this); | ||||||
|             decl.value->visit(*this); |             decl.value->visit(*this); | ||||||
|  | @ -463,7 +463,7 @@ private: | ||||||
|             auto& start_span = span_for_node(decl.name); |             auto& start_span = span_for_node(decl.name); | ||||||
|             start_span.range.set_start({ decl.name->position().end_line.line_number, decl.name->position().end_line.line_column }); |             start_span.range.set_start({ decl.name->position().end_line.line_number, decl.name->position().end_line.line_column }); | ||||||
|             start_span.range.set_end({ decl.value->position().start_line.line_number, decl.value->position().start_line.line_column }); |             start_span.range.set_end({ decl.value->position().start_line.line_number, decl.value->position().start_line.line_column }); | ||||||
|             start_span.color = m_palette.syntax_punctuation(); |             start_span.attributes.color = m_palette.syntax_punctuation(); | ||||||
|             start_span.data = (void*)static_cast<size_t>(AugmentedTokenKind::OpenParen); |             start_span.data = (void*)static_cast<size_t>(AugmentedTokenKind::OpenParen); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  | @ -89,10 +89,10 @@ void SyntaxHighlighter::highlight_matching_token_pair() | ||||||
|         m_brace_buddies[1].index = index1; |         m_brace_buddies[1].index = index1; | ||||||
|         m_brace_buddies[0].span_backup = buddy0; |         m_brace_buddies[0].span_backup = buddy0; | ||||||
|         m_brace_buddies[1].span_backup = buddy1; |         m_brace_buddies[1].span_backup = buddy1; | ||||||
|         buddy0.background_color = Color::DarkCyan; |         buddy0.attributes.background_color = Color::DarkCyan; | ||||||
|         buddy1.background_color = Color::DarkCyan; |         buddy1.attributes.background_color = Color::DarkCyan; | ||||||
|         buddy0.color = Color::White; |         buddy0.attributes.color = Color::White; | ||||||
|         buddy1.color = Color::White; |         buddy1.attributes.color = Color::White; | ||||||
|         m_editor->update(); |         m_editor->update(); | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -38,19 +38,16 @@ | ||||||
| #include <LibGUI/TextRange.h> | #include <LibGUI/TextRange.h> | ||||||
| #include <LibGUI/UndoStack.h> | #include <LibGUI/UndoStack.h> | ||||||
| #include <LibGfx/Color.h> | #include <LibGfx/Color.h> | ||||||
| #include <LibGfx/Forward.h> | #include <LibGfx/TextAttributes.h> | ||||||
| #include <LibRegex/Regex.h> | #include <LibRegex/Regex.h> | ||||||
| 
 | 
 | ||||||
| namespace GUI { | namespace GUI { | ||||||
| 
 | 
 | ||||||
| struct TextDocumentSpan { | struct TextDocumentSpan { | ||||||
|     TextRange range; |     TextRange range; | ||||||
|     Color color; |     Gfx::TextAttributes attributes; | ||||||
|     Optional<Color> background_color; |  | ||||||
|     bool is_skippable { false }; |  | ||||||
|     bool is_underlined { false }; |  | ||||||
|     bool bold { false }; |  | ||||||
|     void* data { nullptr }; |     void* data { nullptr }; | ||||||
|  |     bool is_skippable { false }; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| class TextDocument : public RefCounted<TextDocument> { | class TextDocument : public RefCounted<TextDocument> { | ||||||
|  |  | ||||||
|  | @ -496,13 +496,13 @@ void TextEditor::paint_event(PaintEvent& event) | ||||||
|                     for (auto& span : document().spans()) { |                     for (auto& span : document().spans()) { | ||||||
|                         if (!span.range.contains(physical_position)) |                         if (!span.range.contains(physical_position)) | ||||||
|                             continue; |                             continue; | ||||||
|                         color = span.color; |                         color = span.attributes.color; | ||||||
|                         if (span.bold) { |                         if (span.attributes.bold) { | ||||||
|                             if (auto bold_font = Gfx::FontDatabase::the().get(font->family(), font->presentation_size(), 700)) |                             if (auto bold_font = Gfx::FontDatabase::the().get(font->family(), font->presentation_size(), 700)) | ||||||
|                                 font = bold_font; |                                 font = bold_font; | ||||||
|                         } |                         } | ||||||
|                         background_color = span.background_color; |                         background_color = span.attributes.background_color; | ||||||
|                         underline = span.is_underlined; |                         underline = span.attributes.underline; | ||||||
|                         break; |                         break; | ||||||
|                     } |                     } | ||||||
|                     character_rect.set_width(font->glyph_width(code_point) + font->glyph_spacing()); |                     character_rect.set_width(font->glyph_width(code_point) + font->glyph_spacing()); | ||||||
|  |  | ||||||
							
								
								
									
										41
									
								
								Libraries/LibGfx/TextAttributes.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								Libraries/LibGfx/TextAttributes.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,41 @@ | ||||||
|  | /*
 | ||||||
|  |  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> | ||||||
|  |  * All rights reserved. | ||||||
|  |  * | ||||||
|  |  * Redistribution and use in source and binary forms, with or without | ||||||
|  |  * modification, are permitted provided that the following conditions are met: | ||||||
|  |  * | ||||||
|  |  * 1. Redistributions of source code must retain the above copyright notice, this | ||||||
|  |  *    list of conditions and the following disclaimer. | ||||||
|  |  * | ||||||
|  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | ||||||
|  |  *    this list of conditions and the following disclaimer in the documentation | ||||||
|  |  *    and/or other materials provided with the distribution. | ||||||
|  |  * | ||||||
|  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||||||
|  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||||||
|  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||||||
|  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||||||
|  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||||||
|  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||||||
|  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||||||
|  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||||||
|  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||||||
|  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | #pragma once | ||||||
|  | 
 | ||||||
|  | #include <AK/Optional.h> | ||||||
|  | #include <LibGfx/Color.h> | ||||||
|  | 
 | ||||||
|  | namespace Gfx { | ||||||
|  | 
 | ||||||
|  | struct TextAttributes { | ||||||
|  |     Color color; | ||||||
|  |     Optional<Color> background_color; | ||||||
|  |     bool underline { false }; | ||||||
|  |     bool bold { false }; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | } | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling