Skip to content

Commit

Permalink
fix highlight trigger for macros
Browse files Browse the repository at this point in the history
does not work for math highlights
  • Loading branch information
sunderme committed Jan 11, 2025
1 parent 11a6d46 commit 21e8cee
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
18 changes: 17 additions & 1 deletion src/latexeditorview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,23 @@ bool DefaultInputBinding::runMacros(QKeyEvent *event, QEditor *editor)
foreach (const Macro &m, completerConfig->userMacros) {
if (!m.isActiveForTrigger(Macro::ST_REGEX)) continue;
if (!m.isActiveForLanguage(language)) continue;
if (!(m.isActiveForFormat(line.getFormatAt(column)) || (column > 0 && m.isActiveForFormat(line.getFormatAt(column - 1))))) continue; //two checks, so it works at beginning and end of an environment
if(m.hasFormatTriggers()){
// check formats at column from overlays
QList<int> formats=m.getFormatExcludeTriggers();
if(!formats.isEmpty()){
QFormatRange fr = line.getOverlayAt(column, formats);
if(fr.isValid()){
continue;
}
}
formats=m.getFormatTriggers();
if(!formats.isEmpty()){
QFormatRange fr = line.getOverlayAt(column, formats);
if(!fr.isValid()){
continue;
}
}
}
const QRegularExpression &r = m.triggerRegex;
QRegularExpressionMatch match=r.match(prev);
if (match.hasMatch()) {
Expand Down
28 changes: 24 additions & 4 deletions src/usermacro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,31 @@ bool Macro::isActiveForLanguage(QLanguageDefinition *lang) const
return triggerLanguage.isEmpty() || triggerLanguages.contains(lang);
}

bool Macro::isActiveForFormat(int format) const
/*!
* \brief check if any format trigger are set
* Included or excluded formats are considered
* \return
*/
bool Macro::hasFormatTriggers() const
{
if (!triggerFormatsUnprocessed.isEmpty() || !triggerFormatExcludesUnprocessed.isEmpty()) (const_cast<Macro *>(this))->initTriggerFormats();
// if no trigger format is specified, the macro is active for all formats.
return (triggerFormats.isEmpty() || triggerFormats.contains(format)) && (!triggerFormatExcludes.contains(format));
if (!triggerFormatsUnprocessed.isEmpty() || !triggerFormatExcludesUnprocessed.isEmpty()) (const_cast<Macro *>(this))->initTriggerFormats();
return !triggerFormats.isEmpty() || !triggerFormatExcludes.isEmpty();
}
/*!
* \brief return the list of format triggers
* \return
*/
QList<int> Macro::getFormatTriggers() const
{
return triggerFormats;
}
/*!
* \brief return the list of format triggers which should be excluded
* \return
*/
QList<int> Macro::getFormatExcludeTriggers() const
{
return triggerFormatExcludes;
}

bool Macro::save(const QString &fileName) const {
Expand Down
4 changes: 3 additions & 1 deletion src/usermacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class Macro
void parseTriggerLanguage(QLanguageFactory *langFactory);
bool isActiveForTrigger(SpecialTrigger trigger) const;
bool isActiveForLanguage(QLanguageDefinition *lang) const;
bool isActiveForFormat(int format) const;
bool hasFormatTriggers() const;
QList<int>getFormatTriggers() const;
QList<int>getFormatExcludeTriggers() const;

bool load(const QString &fileName);
bool loadFromText(const QString &text);
Expand Down

0 comments on commit 21e8cee

Please sign in to comment.