Skip to content

Commit

Permalink
hide grammar errors in math env again
Browse files Browse the repository at this point in the history
  • Loading branch information
sunderme committed Dec 24, 2024
1 parent cbefe2d commit e171cf9
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 14 deletions.
12 changes: 12 additions & 0 deletions src/latexdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3046,6 +3046,16 @@ void LatexDocument::gatherCompletionFiles(QStringList &files, QStringList &loade
}
}

void LatexDocument::setHideNonTextGrammarErrors(bool hide)
{
m_hideNonTextGrammarErrors=hide;
}

void LatexDocument::setGrammarFormats(const QList<int> &formats)
{
m_grammarFormats = formats;
}

QString LatexDocument::getMagicComment(const QString &name) const
{
QString seName;
Expand Down Expand Up @@ -3335,6 +3345,8 @@ void LatexDocument::updateSettings()
fmtList.insert(elem.first,getFormatId(elem.second));
}
synChecker.setFormats(fmtList);
synChecker.setHideNonTextGrammarErrors(m_hideNonTextGrammarErrors);
synChecker.setNonTextGrammarFormats(m_grammarFormats);
}

void LatexDocument::checkNextLine(QDocumentLineHandle *dlh, bool clearOverlay, int ticket, int hint)
Expand Down
6 changes: 6 additions & 0 deletions src/latexdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ class LatexDocument: public QDocument

std::list<StructureEntry *> docStructure;

void setHideNonTextGrammarErrors(bool hide);
void setGrammarFormats(const QList<int> &formats);

private:
QString fileName; //absolute
QString temporaryFileName; //absolute, temporary
Expand Down Expand Up @@ -283,6 +286,9 @@ class LatexDocument: public QDocument

bool m_cachedDataOnly=false;

bool m_hideNonTextGrammarErrors=true;
QList<int> m_grammarFormats;

#ifndef QT_NO_DEBUG
public:
QSet<StructureEntry *> StructureContent;
Expand Down
11 changes: 9 additions & 2 deletions src/latexeditorview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,8 @@ void LatexEditorView::removeTemporaryHighlight()

void LatexEditorView::displayLineGrammarErrorsInternal(int lineNr, const QList<GrammarError> &errors)
{
const QList<int> nonTextFormats = {numbersFormat, verbatimFormat, pictureFormat, pweaveDelimiterFormat, pweaveBlockFormat,
sweaveDelimiterFormat, sweaveBlockFormat, math_DelimiterFormat, asymptoteBlockFormat};
QDocumentLine line = document->line(lineNr);
foreach (const int f, grammarFormats)
line.clearOverlays(f);
Expand All @@ -1185,8 +1187,11 @@ void LatexEditorView::displayLineGrammarErrorsInternal(int lineNr, const QList<G
if (grammarFormatsDisabled[index]) continue;
f = grammarFormats[index];
}
if (config->hideNonTextGrammarErrors && (isNonTextFormat(line.getFormatAt(error.offset)) || isNonTextFormat(line.getFormatAt(error.offset + error.length - 1))))
continue;
if (config->hideNonTextGrammarErrors){
QFormatRange overlays=line.getOverlayAt(error.offset,nonTextFormats);
if(overlays.length>0)
continue;
}
line.addOverlay(QFormatRange(error.offset, error.length, f));
}
//todo: check for width changing like if (changed && ff->format(wordRepetitionFormat).widthChanging()) line.handle()->updateWrapAndNotifyDocument(i);
Expand Down Expand Up @@ -1827,6 +1832,8 @@ void LatexEditorView::updateSettings()
QDocument::setWorkAround(QDocument::ForceSingleCharacterDrawing, config->hackRenderingMode == 2);
LatexDocument::syntaxErrorFormat = syntaxErrorFormat;
if (document){
document->setHideNonTextGrammarErrors(config->hideNonTextGrammarErrors);
document->setGrammarFormats(grammarFormats);
document->updateSettings();
document->setCenterDocumentInEditor(config->centerDocumentInEditor);
}
Expand Down
63 changes: 53 additions & 10 deletions src/qcodeedit/lib/document/qdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2961,10 +2961,19 @@ void QDocumentLineHandle::addOverlayNoLock(const QFormatRange& over)
void QDocumentLineHandle::removeOverlay(const QFormatRange& over)
{
QWriteLocker locker(&mLock);
int i = m_overlays.removeAll(over);
removeOverlayNoLock(over);
}
/*!
* \brief remove overlay with extra write locking
* Lock needs to be held by calling function
* \param over
*/
void QDocumentLineHandle::removeOverlayNoLock(const QFormatRange &over)
{
int i = m_overlays.removeAll(over);

if ( i )
setFlag(QDocumentLine::FormatsApplied, false);
if ( i )
setFlag(QDocumentLine::FormatsApplied, false);
}

bool QDocumentLineHandle::hasOverlay(int id){
Expand All @@ -2979,15 +2988,37 @@ bool QDocumentLineHandle::hasOverlay(int id){

QList<QFormatRange> QDocumentLineHandle::getOverlays(int preferredFormat) const {
QReadLocker locker(&mLock);
QList<QFormatRange> result;
if (preferredFormat==-1) {
return m_overlays;
}
return getOverlaysNoLock(preferredFormat);
}

for (int i=0;i<m_overlays.size();i++)
if (m_overlays[i].format==preferredFormat) result.append(m_overlays[i]);
QList<QFormatRange> QDocumentLineHandle::getOverlaysNoLock(int preferredFormat) const
{
QList<QFormatRange> result;
if (preferredFormat==-1) {
return m_overlays;
}

return result;
for (int i=0;i<m_overlays.size();i++)
if (m_overlays[i].format==preferredFormat) result.append(m_overlays[i]);

return result;
}
/*!
* \brief return all overlays of which its format are in the list of preferredFormats
* \param preferredFormats
* \return
*/
QList<QFormatRange> QDocumentLineHandle::getOverlaysNoLock(QList<int> preferredFormats) const
{
QList<QFormatRange> result;
if (preferredFormats.isEmpty()) {
return m_overlays;
}

for (int i=0;i<m_overlays.size();i++)
if (preferredFormats.contains(m_overlays[i].format)) result.append(m_overlays[i]);

return result;
}

QFormatRange QDocumentLineHandle::getOverlayAt(int index, int preferredFormat) const {
Expand All @@ -3001,6 +3032,18 @@ QFormatRange QDocumentLineHandle::getOverlayAt(int index, int preferredFormat) c
return best;
}

QFormatRange QDocumentLineHandle::getOverlayAt(int index, QList<int> preferredFormats) const
{
QReadLocker locker(&mLock);

QFormatRange best;
foreach (QFormatRange fr, m_overlays)
if (fr.offset<=index && fr.offset+fr.length>=index && (preferredFormats.contains(fr.format) || preferredFormats.isEmpty()))
if (best.length<fr.length) best=fr;

return best;
}

QFormatRange QDocumentLineHandle::getFirstOverlay(int start, int end, int preferredFormat) const {
QReadLocker locker(&mLock);

Expand Down
8 changes: 8 additions & 0 deletions src/qcodeedit/lib/document/qdocumentline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,14 @@ QFormatRange QDocumentLine::getOverlayAt(int index, int preferredFormat) const {
return m_handle->getOverlayAt(index,preferredFormat);
}

QFormatRange QDocumentLine::getOverlayAt(int index, QList<int> preferredFormats) const
{
if ( !m_handle )
return QFormatRange();

return m_handle->getOverlayAt(index,preferredFormats);
}

/*!
* \brief Returns the first QFormatRange between columns \a start and \a end for the given \a preferredFormat.
* Any format will match for \a preferredFormat == -1.
Expand Down
1 change: 1 addition & 0 deletions src/qcodeedit/lib/document/qdocumentline.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class QCE_EXPORT QDocumentLine
bool hasOverlay(int id);
QList<QFormatRange> getOverlays(int preferredFormat = -1) const;
QFormatRange getOverlayAt(int index, int preferredFormat = -1) const;
QFormatRange getOverlayAt(int index, QList<int> preferredFormats) const;
QFormatRange getFirstOverlay(int start = 0, int end = -1, int preferredFormat = -1) const;
QFormatRange getLastOverlay(int start = 0, int end = -1, int preferredFormat = -1) const;

Expand Down
4 changes: 4 additions & 0 deletions src/qcodeedit/lib/document/qdocumentline_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,13 @@ class QCE_EXPORT QDocumentLineHandle
void addOverlay(const QFormatRange& over);
void addOverlayNoLock(const QFormatRange& over);
void removeOverlay(const QFormatRange& over);
void removeOverlayNoLock(const QFormatRange& over);
bool hasOverlay(int id);
QList<QFormatRange> getOverlays(int preferredFormat) const;
QList<QFormatRange> getOverlaysNoLock(int preferredFormat) const;
QList<QFormatRange> getOverlaysNoLock(QList<int> preferredFormats) const;
QFormatRange getOverlayAt(int index, int preferredFormat) const;
QFormatRange getOverlayAt(int index, QList<int> preferredFormats) const;
QFormatRange getFirstOverlay(int start = 0, int end = -1, int preferredFormat = -1) const;
QFormatRange getLastOverlay(int start = 0, int end = -1, int preferredFormat = -1) const;

Expand Down
36 changes: 34 additions & 2 deletions src/syntaxcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ void SyntaxCheck::run()
speller=newSpeller;
mReplacementList=newReplacementList;
mFormatList=newFormatList;
m_nonTextGrammarFormats=m_newNonTextGrammarFormats;
}
mLtxCommandLock.unlock();
}
Expand Down Expand Up @@ -151,14 +152,26 @@ void SyntaxCheck::run()
newLine.dlh->lockForWrite();
if (newLine.ticket == newLine.dlh->getCurrentTicket()) { // discard results if text has been changed meanwhile
newLine.dlh->setCookie(QDocumentLine::LEXER_COOKIE,QVariant::fromValue<TokenList>(tl));
QList<QFormatRange>grammarOverlays=newLine.dlh->getOverlaysNoLock(m_nonTextGrammarFormats);
foreach (const Error &elem, newRanges){
if(!mSyntaxChecking && (elem.type!=ERR_spelling) && (elem.type!=ERR_highlight) ){
// skip all syntax errors
continue;
}
int fmt= elem.type == ERR_spelling ? SpellerUtility::spellcheckErrorFormat : syntaxErrorFormat;
fmt= elem.type == ERR_highlight ? elem.format : fmt;
int fmt= (elem.type == ERR_spelling) ? SpellerUtility::spellcheckErrorFormat : syntaxErrorFormat;
fmt= (elem.type == ERR_highlight) ? elem.format : fmt;
newLine.dlh->addOverlayNoLock(QFormatRange(elem.range.first, elem.range.second, fmt));
// for ERR_highlight, remove grammarErrors
if(m_hideNonTextGrammarErrors && elem.type==ERR_highlight){
for(int i = 0; i<grammarOverlays.size();++i){
const QFormatRange &range=grammarOverlays.at(i);
if(range.offset>=elem.range.first && range.offset<=elem.range.second){
newLine.dlh->removeOverlayNoLock(range);
grammarOverlays.removeAt(i);
--i;
}
}
}
}
// add comment hightlight if present
if(commentStart.first>=0){
Expand Down Expand Up @@ -291,6 +304,25 @@ void SyntaxCheck::enableSyntaxCheck(const bool enable){
if (stopped) return;
mSyntaxChecking=enable;
}
/*! \brief hide non text spelling errors
* \param hide
*/
void SyntaxCheck::setHideNonTextGrammarErrors(const bool hide)
{
m_hideNonTextGrammarErrors=hide;
}
/*!
* \brief SyntaxCheck::setNonTextGrammarFormats
* \param formats
*/
void SyntaxCheck::setNonTextGrammarFormats(const QList<int> formats)
{
if (stopped) return;
mLtxCommandLock.lock();
newLtxCommandsAvailable = true;
m_newNonTextGrammarFormats=formats;
mLtxCommandLock.unlock();
}
/*!
* \brief set character/text replacementList for spell checking
* \param replacementList Map for characater/text replacement prior to spellchecking words. E.g. "u -> ü when german is activated
Expand Down
4 changes: 4 additions & 0 deletions src/syntaxcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class SyntaxCheck : public SafeThread
void setReplacementList(QMap<QString, QString> replacementList);
void setFormats(QMap<QString, int> formatList);
void enableSyntaxCheck(const bool enable);
void setHideNonTextGrammarErrors(const bool hide);
void setNonTextGrammarFormats(const QList<int> formats);

void markUnclosedEnv(Environment env);

Expand All @@ -135,6 +137,8 @@ class SyntaxCheck : public SafeThread
bool stopped;
bool mSyntaxChecking; //! show/hide syntax errors
int syntaxErrorFormat;
bool m_hideNonTextGrammarErrors=true;
QList<int> m_nonTextGrammarFormats,m_newNonTextGrammarFormats;
QSharedPointer<LatexParser> ltxCommands;

QSharedPointer<LatexParser> newLtxCommands;
Expand Down

0 comments on commit e171cf9

Please sign in to comment.