Skip to content

Commit

Permalink
handle specialDefinition better on changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sunderme committed Nov 4, 2023
1 parent ec4bdaf commit 60fbd2b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
71 changes: 45 additions & 26 deletions src/latexdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -816,9 +816,6 @@ void LatexDocument::interpretCommandArguments(QDocumentLineHandle *dlh, const in
cs.type = CodeSnippet::length;
mUserCommandList.insert(dlh, UserCommandPair(cmdNameWithoutArgs, cs));
}

// remove obsolete Overlays (maybe this can be refined
//updateSyntaxCheck=true;
continue;
}

Expand Down Expand Up @@ -1149,20 +1146,32 @@ void LatexDocument::handleRescanDocuments(HandledData changedCommands){
loopAgain=true;
updateCompleter=true;
}
if(!changedCommands.addedUserCommands.isEmpty()){
lp->possibleCommands["user"].unite(ltxCommands.possibleCommands["user"]);
// handle specialDef commands
for(const QString &key: changedCommands.addedUserCommands){
if(key.startsWith("%")){
int i = key.indexOf('%', 1);
QString category = key.left(i);
QString elem = key.mid(i + 1);
lp->possibleCommands[category].insert(elem);
ltxCommands.possibleCommands[category].insert(elem);
}
}
if(!changedCommands.addedUserCommands.isEmpty()){
lp->possibleCommands["user"].unite(ltxCommands.possibleCommands["user"]);
// handle specialDef commands
for(const QString &key: changedCommands.addedUserCommands){
if(key.startsWith("%")){
int i = key.indexOf('%', 1);
QString category = key.left(i);
QString elem = key.mid(i + 1);
lp->possibleCommands[category].insert(elem);
ltxCommands.possibleCommands[category].insert(elem);
}
}
}
if(!changedCommands.removedUserCommands.isEmpty()){
for(const QString &key: changedCommands.removedUserCommands){
if(key.startsWith("%")){
int i = key.indexOf('%', 1);
QString category = key.left(i);
QString elem = key.mid(i + 1);
lp->possibleCommands[category].remove(elem);
ltxCommands.possibleCommands[category].remove(elem);
}
}
}

synChecker.setLtxCommands(lp); // redundant here, updateCompletionfiles
reCheckSyntax();
}
Expand Down Expand Up @@ -1206,16 +1215,24 @@ void LatexDocument::removeLineElements(QDocumentLineHandle *dlh, HandledData &ch
}
}
}else{
int i = elem.indexOf("{");
if (i >= 0) elem = elem.left(i);
if(countCommandDefintions(elem)==1){
ltxCommands.possibleCommands["user"].remove(elem);
if(elem.startsWith("%")){
int i = elem.indexOf('%', 1);
QString category = elem.left(i);
QString wrd = elem.mid(i + 1);
if(countCommandDefintions("",elem)==1){
ltxCommands.possibleCommands[category].remove(wrd);
}
}else{
int i = elem.indexOf("{");
if (i >= 0) elem = elem.left(i);
if(countCommandDefintions(elem)==1){
ltxCommands.possibleCommands["user"].remove(elem);
}
}
}
if(cmd.snippet.type==CodeSnippet::userConstruct)
continue;
changedCommands.removedUserCommands << elem;
//updateSyntaxCheck=true;
}
if (mLabelItem.contains(dlh)) {
QList<ReferencePair> labels = mLabelItem.values(dlh);
Expand Down Expand Up @@ -1586,12 +1603,18 @@ LatexDocument* LatexDocument::getDocumentForLabel(const QString &name){
return nullptr;
}

int LatexDocument::countCommandDefintions(const QString &name)
int LatexDocument::countCommandDefintions(const QString &name,const QString word)
{
int result=0;
for (auto it = mUserCommandList.constBegin(); it != mUserCommandList.constEnd(); ++it) {
if (it.value().name == name) {
++result;
if(name.isEmpty()){
if (it.value().snippet.word == word) {
++result;
}
}else{
if (it.value().name == name) {
++result;
}
}
}
return result;
Expand Down Expand Up @@ -2883,10 +2906,6 @@ bool LatexDocument::updateCompletionFiles(const bool updatePackages, const bool
ltxCommands.possibleCommands[category].insert(elem);
continue;
}
if (!elem.startsWith("\\begin{") && !elem.startsWith("\\end{")) {
int i = elem.indexOf(QRegularExpression("\\W"), 1);
if (i >= 0) elem = elem.left(i);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/latexdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class LatexDocument: public QDocument
Q_INVOKABLE QMultiHash<QDocumentLineHandle *, int> getRefs(const QString &name); ///< get line/column from reference name
Q_INVOKABLE QMultiHash<QDocumentLineHandle *, int> getBibItems(const QString &name);
LatexDocument *getDocumentForLabel(const QString &name); ///< get document from label name
int countCommandDefintions(const QString &name); ///< count how many time a certain command is defined, used for checking for duplicates
int countCommandDefintions(const QString &name, const QString word=""); ///< count how many time a certain command is defined, used for checking for duplicates
Q_INVOKABLE QDocumentLineHandle *findCommandDefinition(const QString &name); ///< get line of definition from command name (may return nullptr)
Q_INVOKABLE QDocumentLineHandle *findUsePackage(const QString &name); ///< get line of \usepackage from package name (may return nullptr)
Q_INVOKABLE void replaceItems(QMultiHash<QDocumentLineHandle *, ReferencePair> items, const QString &newName, QDocumentCursor *cursor = nullptr);
Expand Down

0 comments on commit 60fbd2b

Please sign in to comment.