Skip to content

Commit

Permalink
[Markdown] Highlight hard line breaks, issue #900.
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Nov 2, 2024
1 parent 7a13c74 commit e3124e2
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions scintilla/include/SciLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,7 @@
#define SCE_MARKDOWN_DIFF_ADD_SQUARE 93
#define SCE_MARKDOWN_DIFF_DEL_CURLY 94
#define SCE_MARKDOWN_DIFF_DEL_SQUARE 95
#define SCE_MARKDOWN_HARD_LINE_BREAK 96
#define SCE_AHK_DEFAULT 0
#define SCE_AHK_COMMENTLINE 1
#define SCE_AHK_SECTION_COMMENT 2
Expand Down
1 change: 1 addition & 0 deletions scintilla/include/SciLexer.iface
Original file line number Diff line number Diff line change
Expand Up @@ -2014,6 +2014,7 @@ val SCE_MARKDOWN_DIFF_ADD_CURLY=
val SCE_MARKDOWN_DIFF_ADD_SQUARE=
val SCE_MARKDOWN_DIFF_DEL_CURLY=
val SCE_MARKDOWN_DIFF_DEL_SQUARE=
val SCE_MARKDOWN_HARD_LINE_BREAK=
# Lexical state for SCLEX_TXT2TAGS
#lex Txt2tags=SCLEX_TXT2TAGS SCE_TXT2TAGS_
#val SCE_TXT2TAGS_DEFAULT=0
Expand Down
26 changes: 17 additions & 9 deletions scintilla/lexers/LexMarkdown.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ struct MarkdownLexer {
}

int HighlightBlockText(uint32_t lineState);
void HighlightInlineText();
void HighlightInlineText(int visibleChars = 0);

HtmlTagType CheckHtmlBlockTag(Sci_PositionU pos, Sci_PositionU endPos, int chNext, bool paragraph) const noexcept;
bool HandleHtmlTag(HtmlTagType tagType);
Expand All @@ -310,7 +310,7 @@ struct MarkdownLexer {
bool IsIndentedBlockEnd() const noexcept;

int GetCurrentDelimiterRun(DelimiterRun &delimiterRun, bool ignoreCurrent = false) const noexcept;
SeekStatus HighlightEmphasis(uint32_t lineState);
SeekStatus HighlightEmphasis(uint32_t lineState, int visibleChars);
SeekStatus HighlightCodeSpan(uint32_t lineState);

SeekStatus HighlightLinkText(uint32_t lineState);
Expand Down Expand Up @@ -739,7 +739,7 @@ constexpr uint8_t GetEmphasisDelimiter(int state) noexcept {
}
}

SeekStatus MarkdownLexer::HighlightEmphasis(uint32_t lineState) {
SeekStatus MarkdownLexer::HighlightEmphasis(uint32_t lineState, int visibleChars) {
HighlightResult result = HighlightResult::None;
const int current = sc.state;
const int delimiter = GetEmphasisDelimiter(current);
Expand Down Expand Up @@ -803,7 +803,7 @@ SeekStatus MarkdownLexer::HighlightEmphasis(uint32_t lineState) {
return SeekStatus::Continue;
}

HighlightInlineText();
HighlightInlineText(visibleChars);
return SeekStatus::None;
}

Expand Down Expand Up @@ -1597,7 +1597,7 @@ int MarkdownLexer::HighlightBlockText(uint32_t lineState) {
return 0;
}

void MarkdownLexer::HighlightInlineText() {
void MarkdownLexer::HighlightInlineText(int visibleChars) {
bool handled = false;
const int current = sc.state;
switch (sc.ch) {
Expand Down Expand Up @@ -1741,6 +1741,14 @@ void MarkdownLexer::HighlightInlineText() {
}
if (handled || current != sc.state) {
SaveOuterStyle(current);
} else if (visibleChars != 0
&& (current == SCE_MARKDOWN_DEFAULT || (current >= SCE_MARKDOWN_EM_ASTERISK && current <= SCE_MARKDOWN_STRIKEOUT))
&& ((sc.ch == '\\' && IsEOLChar(sc.chNext)) || (sc.Match(' ', ' ') && IsEOLChar(sc.GetRelative(2))))) {
sc.SetState(SCE_MARKDOWN_HARD_LINE_BREAK);
if (sc.ch == ' ') {
sc.Forward();
}
sc.ForwardSetState(current);
} else if (bracketCount == 0) {
DetectAutoLink();
}
Expand Down Expand Up @@ -2071,7 +2079,7 @@ void ColouriseMarkdownDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int in
case SCE_MARKDOWN_INLINE_MATH: {
SeekStatus status;
if (sc.state < SCE_MARKDOWN_LINK_TEXT) {
status = lexer.HighlightEmphasis(lineState);
status = lexer.HighlightEmphasis(lineState, visibleChars);
} else if (sc.state == SCE_MARKDOWN_LINK_TEXT) {
status = lexer.HighlightLinkText(lineState);
} else if (sc.state < SCE_MARKDOWN_CODE_SPAN) {
Expand Down Expand Up @@ -2363,8 +2371,8 @@ void ColouriseMarkdownDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int in
}

if (sc.state == SCE_MARKDOWN_DEFAULT) {
if (sc.ch > ' ') {
if (visibleBefore == 0) {
if (sc.ch >= ' ') {
if (sc.ch > ' ' && visibleBefore == 0) {
if (indentCurrent != indentPrevious) {
lexer.UpdateParentIndentCount(indentCurrent);
}
Expand All @@ -2382,7 +2390,7 @@ void ColouriseMarkdownDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int in
}
}
if (sc.state == SCE_MARKDOWN_DEFAULT) {
lexer.HighlightInlineText();
lexer.HighlightInlineText(visibleChars);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/EditLexers/EditStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
#define NP2STYLE_Parameter 63603
#define NP2STYLE_InlineExpansion 63604
#define NP2STYLE_Pragma 63605
#define NP2STYLE_HardLineBreak 63606

#define NP2STYLE_Section 63611
#define NP2STYLE_Assignment 63612
Expand Down
1 change: 1 addition & 0 deletions src/EditLexers/EditStyleX.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@
#define NP2StyleX_Parameter EDITSTYLE_HOLE(Parameter, L"Parameter")
#define NP2StyleX_InlineExpansion EDITSTYLE_HOLE(InlineExpansion, L"Inline Expansion")
#define NP2StyleX_Pragma EDITSTYLE_HOLE(Pragma, L"Pragma")
#define NP2StyleX_HardLineBreak EDITSTYLE_HOLE(HardLineBreak, L"Hard Line Break")

#define NP2StyleX_Section EDITSTYLE_HOLE(Section, L"Section")
#define NP2StyleX_Assignment EDITSTYLE_HOLE(Assignment, L"Assignment")
Expand Down
1 change: 1 addition & 0 deletions src/EditLexers/stlMarkdown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static EDITSTYLE Styles_Markdown[] = {
{ SCE_MARKDOWN_DELIMITER, NP2StyleX_Delimiter, L"bold; fore:#0080C0" },
{ SCE_MARKDOWN_EMOJI, NP2StyleX_Emoji, L"fore:#FF8000" },
{ SCE_MARKDOWN_ESCAPECHAR, NP2StyleX_EscapeSequence, L"fore:#0080C0" },
{ SCE_MARKDOWN_HARD_LINE_BREAK, NP2StyleX_HardLineBreak, L"fore:#C80000; back:#FFFF80" },
{ MULTI_STYLE(SCE_MARKDOWN_DIFF_ADD_CURLY, SCE_MARKDOWN_DIFF_ADD_SQUARE, 0, 0), NP2StyleX_LineAddition, L"back:#80FF80" },
{ MULTI_STYLE(SCE_MARKDOWN_DIFF_DEL_CURLY, SCE_MARKDOWN_DIFF_DEL_SQUARE, 0, 0), NP2StyleX_LineRemoval, L"back:#FF8080" },
};
Expand Down

0 comments on commit e3124e2

Please sign in to comment.