From 31a69cf3bd5ec55c981bebc8d0a4f1bd08cfcfab Mon Sep 17 00:00:00 2001 From: "Michael G. Kazakov" Date: Sat, 8 Jun 2024 21:40:01 +0100 Subject: [PATCH] Added highlighting settings for YAML and Bash (#297) * Added settings for YAML highlighting * Added styling settings for Bash, added red and yellow colors for warning/error/critical messages in the log window --- .../Resources/SyntaxHighlighting/Bash.json | 29 ++++++++++++ .../Resources/SyntaxHighlighting/Main.json | 12 ++++- .../Resources/SyntaxHighlighting/YAML.json | 20 +++++++++ Source/Utility/source/SpdLogWindow.mm | 45 +++++++++++++++++-- 4 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 Source/NimbleCommander/NimbleCommander/Resources/SyntaxHighlighting/Bash.json create mode 100644 Source/NimbleCommander/NimbleCommander/Resources/SyntaxHighlighting/YAML.json diff --git a/Source/NimbleCommander/NimbleCommander/Resources/SyntaxHighlighting/Bash.json b/Source/NimbleCommander/NimbleCommander/Resources/SyntaxHighlighting/Bash.json new file mode 100644 index 000000000..03279fb21 --- /dev/null +++ b/Source/NimbleCommander/NimbleCommander/Resources/SyntaxHighlighting/Bash.json @@ -0,0 +1,29 @@ +{ + "lexer": "bash", + "wordlists": [ + "if then elif else fi time for in until while do done case esac coproc select function" + ], + "properties": { + "lexer.bash.styling.inside.string": "1", + "lexer.bash.styling.inside.backticks": "1", + "lexer.bash.styling.inside.parameter": "1", + "lexer.bash.styling.inside.heredoc": "1", + "lexer.bash.command.substitution": "1" + }, + "mapping": { + "SCE_SH_DEFAULT": "default", + "SCE_SH_ERROR": "preprocessor", + "SCE_SH_COMMENTLINE": "comment", + "SCE_SH_NUMBER": "number", + "SCE_SH_WORD": "keyword", + "SCE_SH_STRING": "string", + "SCE_SH_CHARACTER": "string", + "SCE_SH_OPERATOR": "operator", + "SCE_SH_IDENTIFIER": "identifier", + "SCE_SH_SCALAR": "keyword", + "SCE_SH_PARAM": "keyword", + "SCE_SH_BACKTICKS": "operator", + "SCE_SH_HERE_DELIM": "operator", + "SCE_SH_HERE_Q": "operator" + } +} diff --git a/Source/NimbleCommander/NimbleCommander/Resources/SyntaxHighlighting/Main.json b/Source/NimbleCommander/NimbleCommander/Resources/SyntaxHighlighting/Main.json index f671ea147..eb0832d0e 100644 --- a/Source/NimbleCommander/NimbleCommander/Resources/SyntaxHighlighting/Main.json +++ b/Source/NimbleCommander/NimbleCommander/Resources/SyntaxHighlighting/Main.json @@ -1,5 +1,10 @@ { - "langs": [ + "langs": [ + { + "name": "Bash", + "settings": "Bash.json", + "filemask": "*.sh" + }, { "name": "C++", "settings": "C++.json", @@ -9,6 +14,11 @@ "name": "JSON", "settings": "JSON.json", "filemask": "*.json" + }, + { + "name": "YAML", + "settings": "YAML.json", + "filemask": "*.yaml, *.yml" } ] } diff --git a/Source/NimbleCommander/NimbleCommander/Resources/SyntaxHighlighting/YAML.json b/Source/NimbleCommander/NimbleCommander/Resources/SyntaxHighlighting/YAML.json new file mode 100644 index 000000000..7b3122676 --- /dev/null +++ b/Source/NimbleCommander/NimbleCommander/Resources/SyntaxHighlighting/YAML.json @@ -0,0 +1,20 @@ +{ + "lexer": "yaml", + "wordlists": [ + "true false yes no" + ], + "properties": { + }, + "mapping": { + "SCE_YAML_DEFAULT": "default", + "SCE_YAML_COMMENT": "comment", + "SCE_YAML_IDENTIFIER": "keyword", + "SCE_YAML_KEYWORD": "keyword", + "SCE_YAML_NUMBER": "number", + "SCE_YAML_REFERENCE": "identifier", + "SCE_YAML_DOCUMENT": "keyword", + "SCE_YAML_TEXT": "string", + "SCE_YAML_ERROR": "preprocessor", + "SCE_YAML_OPERATOR": "operator" + } +} diff --git a/Source/Utility/source/SpdLogWindow.mm b/Source/Utility/source/SpdLogWindow.mm index 2aade5211..cc9aea06e 100644 --- a/Source/Utility/source/SpdLogWindow.mm +++ b/Source/Utility/source/SpdLogWindow.mm @@ -1,4 +1,4 @@ -// Copyright (C) 2022-2023 Michael Kazakov. Subject to GNU General Public License version 3. +// Copyright (C) 2022-2024 Michael Kazakov. Subject to GNU General Public License version 3. #include "SpdLogWindow.h" #include #include @@ -98,6 +98,9 @@ @implementation NCSpdLogWindowController { std::shared_ptr m_Sink; NSTimer *m_DrainTimer; NSDictionary *m_TextAttrs; + NSDictionary *m_WarningAttrs; + NSDictionary *m_ErrorAttrs; + NSDictionary *m_CriticalAttrs; bool m_AutoScroll; } @@ -218,8 +221,21 @@ - (void)createControls m_TextAttrs = @{ NSFontAttributeName: [NSFont monospacedSystemFontOfSize:10. weight:NSFontWeightRegular], - NSForegroundColorAttributeName: [NSColor textColor] + NSForegroundColorAttributeName: NSColor.textColor }; + m_WarningAttrs = @{ + NSFontAttributeName: [NSFont monospacedSystemFontOfSize:10. weight:NSFontWeightSemibold], + NSForegroundColorAttributeName: NSColor.systemYellowColor + }; + m_ErrorAttrs = @{ + NSFontAttributeName: [NSFont monospacedSystemFontOfSize:10. weight:NSFontWeightSemibold], + NSForegroundColorAttributeName: NSColor.systemRedColor + }; + m_CriticalAttrs = @{ + NSFontAttributeName: [NSFont monospacedSystemFontOfSize:10. weight:NSFontWeightBold], + NSForegroundColorAttributeName: NSColor.systemRedColor + }; + m_ScrollView = sv; m_TextView = tv; m_TextStorage = tv.textStorage; @@ -244,11 +260,34 @@ + (NSWindow *)makeWindow return wnd; } +- (void)highlightString:(NSMutableAttributedString *)_str + withAttributes:(NSDictionary *)_attrs + forSubstring:(NSString *)_sub_str +{ + NSString *str = _str.string; + const size_t length = str.length; + + NSRange search_range = NSMakeRange(0, str.length); + NSRange found; + while( (found = [str rangeOfString:_sub_str options:0 range:search_range]).location != NSNotFound ) { + [_str setAttributes:_attrs range:found]; + search_range = NSMakeRange(NSMaxRange(found), length - NSMaxRange(found)); + } +} + +- (void)highlightString:(NSMutableAttributedString *)_str +{ + [self highlightString:_str withAttributes:m_WarningAttrs forSubstring:@" [warning] "]; + [self highlightString:_str withAttributes:m_ErrorAttrs forSubstring:@" [error] "]; + [self highlightString:_str withAttributes:m_CriticalAttrs forSubstring:@" [critical] "]; +} + - (void)acceptNewString:(NSString *)_str { dispatch_assert_main_queue(); assert(_str != nil); - if( auto as = [[NSAttributedString alloc] initWithString:_str attributes:m_TextAttrs] ) { + if( auto as = [[NSMutableAttributedString alloc] initWithString:_str attributes:m_TextAttrs] ) { + [self highlightString:as]; [m_TextStorage appendAttributedString:as]; if( m_AutoScroll ) [m_TextView scrollToEndOfDocument:nil];