Skip to content

Commit

Permalink
Merge pull request #428 from antonvw/317-improve-lilypond-lexer
Browse files Browse the repository at this point in the history
317 improve lilypond lexer
  • Loading branch information
antonvw authored Aug 31, 2023
2 parents d13aac6 + e599d1c commit 2aeafc8
Show file tree
Hide file tree
Showing 9 changed files with 926 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- stc::vi_command: argument now takes wex::line_data
- removed some not necessary copy constructors
- moved window.h and control.h to factory
- lilypond lexer improved

### Fixed
- ctags::find and empty tag finds next tag
Expand Down
16 changes: 16 additions & 0 deletions data/wex-lexers-macro.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,22 @@ Copyright: (c) 2009-2023, Anton van Wezenbeek
<def no ="error"/>
</def>

<def name = "lilypond">
<def no ="default"/>
<def no ="command"/>
<def no ="tag"/>
<def no ="math"/>
<def no ="comment"/>
<def no ="tag2"/>
<def no ="math2"/>
<def no ="comment2"/>
<def no ="verbatim"/>
<def no ="shortcmd"/>
<def no ="special"/>
<def no ="cmdopt"/>
<def no ="error"/>
</def>

<def name = "lisp">
<def no ="default"/>
<def no ="comment"/>
Expand Down
16 changes: 13 additions & 3 deletions data/wex-lexers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1216,14 +1216,19 @@ www smtp snmp snmp-server ipv4 ipv6 tftp ftp echo dscp isakmp nntp gopher pop3 i
</lexer>
<lexer name="kix"/>
<lexer name="kvirc"/>
<lexer name="latex" extensions="*.tex">
<lexer name="latex" extensions="*.latex">
<properties>
<property name="fold"> 1 </property>
<property name="fold.compact"> 1 </property>
<property name="fold.comment"> 1 </property>
</properties>
</lexer>
<lexer display="lilypond" name="latex" extensions="*.ly">
<lexer name="lilypond" extensions="*.ly">
<properties>
<property name="fold"> 1 </property>
<property name="fold.compact"> 1 </property>
<property name="fold.comment"> 1 </property>
</properties>
</lexer>
<lexer name="lisp">
<properties>
Expand Down Expand Up @@ -1326,6 +1331,11 @@ www smtp snmp snmp-server ipv4 ipv6 tftp ftp echo dscp isakmp nntp gopher pop3 i
</lexer>
<lexer name="nim"/>
<lexer name="nimrod" macro="python"/>
<lexer display="ninja" extensions="*.ninja" edgemode="none" spacevisible="invisible" name="bash">
<keywords>
build default include
</keywords>
</lexer>
<lexer name="nncrontab"/>
<lexer name="nsis"/>
<lexer name="octave" macro="matlab"/>
Expand Down Expand Up @@ -1568,7 +1578,7 @@ www smtp snmp snmp-server ipv4 ipv6 tftp ftp echo dscp isakmp nntp gopher pop3 i
</keywords>
</lexer>
<lexer name="tehex" macro="ihex"/>
<lexer name="tex"/>
<lexer name="tex" extensions="*.tex"/>
<lexer name="txt2tags"/>
<lexer name="vb">
<keywords set ="vb"/>
Expand Down
3 changes: 3 additions & 0 deletions external/lexilla.patch
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ index 07dc6c1c..2e9eacdf 100644
using namespace Scintilla;

+#include "../../../../../../src/syntax/wex/lex-rfw.cpp"
+#include "../../../../../../src/syntax/wex/lex-lilypond.cpp"
+
#define HERE_DELIM_MAX 256

Expand All @@ -19,6 +20,7 @@ index bd15d392..8ecb9599 100644
extern LexerModule lmRaku;
extern LexerModule lmREBOL;
extern LexerModule lmRegistry;
+extern LexerModule lmLilyPond;
+extern LexerModule lmRFW;
extern LexerModule lmRuby;
extern LexerModule lmRust;
Expand All @@ -27,6 +29,7 @@ index bd15d392..8ecb9599 100644
&lmRaku,
&lmREBOL,
&lmRegistry,
+ &lmLilyPond,
+ &lmRFW,
&lmRuby,
&lmRust,
Expand Down
2 changes: 2 additions & 0 deletions src/ex/vi/commands-motion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,8 @@ void wex::vi::visual_extend(int begin_pos, int end_pos) const

default:
get_stc()->SetCurrentPos(end_pos);
get_stc()
->SelectNone(); // appearently previous call selects to the new pos ..
break;
}
}
219 changes: 219 additions & 0 deletions src/syntax/wex/lex-lilypond-util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
////////////////////////////////////////////////////////////////////////////////
// Name: lex-lilypond-util.h
// Purpose: Implementation of class lilypond
// Author: Anton van Wezenbeek
// Copyright: (c) 2023 Anton van Wezenbeek
////////////////////////////////////////////////////////////////////////////////

#pragma once

namespace Scintilla
{
/// Offers some general methods.
class lilypond
{
public:
/// Static interface

static bool is_letter(int ch);

/// Other methods

/// Constructor.
lilypond(LexAccessor& s)
: m_styler(s)
{
;
}

bool is_special(char& ch, Sci_Position& no) const;
bool is_tag_valid(Sci_Position& i, Sci_Position l) const;

bool last_word_check(
Sci_Position start,
const char* needle,
const std::vector<std::string>& next,
Sci_Position length,
int& state) const;
bool last_word_is(Sci_Position start, const char* needle) const;
bool last_word_is_match_env(Sci_Position pos) const;

bool next_not_blank_is(Sci_Position i, char needle) const;

private:
LexAccessor& m_styler;
};

// inline implementation lex_rfw_access

inline bool lilypond::is_letter(int ch)
{
return IsASCII(ch) && isalpha(ch);
}

inline bool lilypond::is_special(char& ch, Sci_Position& i) const
{
if (
(ch == '#') || (ch == '$') || (ch == '%') || (ch == '&') || (ch == '_') ||
(ch == '{') || (ch == '}') || (ch == ' '))
{
m_styler.ColourTo(i + 1, SCE_L_SPECIAL);
i++;
ch = m_styler.SafeGetCharAt(i + 1);
return true;
}

return false;
}

inline bool lilypond::is_tag_valid(Sci_Position& i, Sci_Position l) const
{
while (i < l)
{
if (m_styler.SafeGetCharAt(i) == '{')
{
while (i < l)
{
i++;
if (m_styler.SafeGetCharAt(i) == '}')
{
return true;
}
else if (
!is_letter(m_styler.SafeGetCharAt(i)) &&
m_styler.SafeGetCharAt(i) != '*')
{
return false;
}
}
}
else if (!isblank(m_styler.SafeGetCharAt(i)))
{
return false;
}
i++;
}
return false;
}

inline bool lilypond::next_not_blank_is(Sci_Position i, char needle) const
{
while (i < m_styler.Length())
{
if (char ch = m_styler.SafeGetCharAt(i); !isspace(ch) && ch != '*')
{
return ch == needle;
}
i++;
}
return false;
}

inline bool lilypond::last_word_check(
Sci_Position start,
const char* needle,
const std::vector<std::string>& checks,
Sci_Position lengthDoc,
int& state) const
{
Sci_Position match = start + 3;

if (last_word_is(match, needle))
{
match++;

if (is_tag_valid(match, lengthDoc))
{
if (checks.empty())
{
if (last_word_is_match_env(match))
{
state = SCE_L_COMMAND;
return true;
}
}
else
{
for (const auto& w : checks)
{
if (last_word_is(match, std::string("{" + w + "}").c_str()))
{
m_styler.ColourTo(start - 1, state);
state = SCE_L_COMMAND;
return true;
}
}
}
}
}

return false;
}

inline bool lilypond::last_word_is(Sci_Position start, const char* needle) const
{
const Sci_PositionU l = static_cast<Sci_PositionU>(strlen(needle));
const Sci_Position ini = start - l + 1;

char s[32];

Sci_PositionU i = 0;
while (i < l && i < 31)
{
s[i] = m_styler.SafeGetCharAt(ini + i);
i++;
}
s[i] = '\0';

return (strcmp(s, needle) == 0);
}

inline bool lilypond::last_word_is_match_env(Sci_Position pos) const
{
Sci_Position i, j;
char s[32];

if (m_styler.SafeGetCharAt(pos) != '}')
return false;

for (i = pos - 1; i >= 0; --i)
{
if (m_styler.SafeGetCharAt(i) == '{')
break;
if (pos - i >= 20)
return false;
}

if (i < 0 || i == pos - 1)
return false;

++i;
for (j = 0; i + j < pos; ++j)
s[j] = m_styler.SafeGetCharAt(i + j);

s[j] = '\0';
if (j == 0)
return false;

if (s[j - 1] == '*')
s[--j] = '\0';

for (const auto& word : std::vector<std::string>{
"align",
"alignat",
"flalign",
"gather",
"multiline",
"displaymath",
"eqnarray",
"equation"})
{
if (word == s)
{
return true;
}
}

return false;
}
}; // namespace Scintilla
Loading

0 comments on commit 2aeafc8

Please sign in to comment.