Skip to content

Commit

Permalink
search cwl next to local packages
Browse files Browse the repository at this point in the history
implement texstudio-org#3883

When a usepackage refers to a local file with a relative path ("."
needed as first character), txs looks for packageName.cwl as the
corresponding cwl file and loads it.
  • Loading branch information
sunderme committed Nov 23, 2024
1 parent a209aee commit c7eb058
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/latexdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3001,6 +3001,12 @@ void LatexDocument::gatherCompletionFiles(QStringList &files, QStringList &loade
found=!zw.containsOptionalSections;
}
if(!found){
if(fileName.startsWith(".")){
// replace relative path with absolute with repsit to root document
QFileInfo fi=getRootDocument()->getFileInfo();
QFileInfo fi_cwl=QFileInfo(fi.absolutePath(),fileName);
fileName=fi_cwl.absoluteFilePath();
}
zw = loadCwlFile(fileName, completerConfig, options);
if (!zw.notFound) {
fileName= zw.containsOptionalSections ? elem : fileName;
Expand Down
28 changes: 25 additions & 3 deletions src/latexeditorview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1646,9 +1646,19 @@ void LatexEditorView::updatePackageFormats()
const QString rpck = trimLeft(curLineText.mid(tk.start, tk.length)); // left spaces are ignored by \cite, right space not
const QString suffix = tk.type == Token::documentclass ? ".cls" : ".sty";
//check and highlight
bool localPackage = false;
if(rpck.startsWith(".")){
// check if file exists
LatexDocument *root=document->getRootDocument();
QFileInfo fi=root->getFileInfo();
QFileInfo fi_cwl=QFileInfo(fi.absolutePath(),rpck+suffix);
localPackage=fi_cwl.exists();
}
if (latexPackageList->empty())
dlh->addOverlay(QFormatRange(tk.start, tk.length, packageUndefinedFormat));
else if ( (latexPackageList->find(preambel + rpck + suffix) != latexPackageList->end()) || (latexPackageList->find(preambel + rpck) != latexPackageList->end())) {
else if ( (latexPackageList->find(preambel + rpck + suffix) != latexPackageList->end())
|| (latexPackageList->find(preambel + rpck) != latexPackageList->end())
|| localPackage) {
dlh->addOverlay(QFormatRange(tk.start, tk.length, packagePresentFormat));
} else {
dlh->addOverlay(QFormatRange(tk.start, tk.length, packageMissingFormat));
Expand Down Expand Up @@ -2211,9 +2221,19 @@ void LatexEditorView::documentContentChanged(int linenr, int count)
const QString rpck = trimLeft(text.mid(tk.start, tk.length)); // left spaces are ignored by \cite, right space not
const QString suffix = tk.type == Token::documentclass ? ".cls" : ".sty";
//check and highlight
bool localPackage = false;
if(rpck.startsWith(".")){
// check if file exists
LatexDocument *root=document->getRootDocument();
QFileInfo fi=root->getFileInfo();
QFileInfo fi_cwl=QFileInfo(fi.absolutePath(),rpck+suffix);
localPackage=fi_cwl.exists();
}
if (latexPackageList->empty())
dlh->addOverlay(QFormatRange(tk.start, tk.length, packageUndefinedFormat));
else if ( (latexPackageList->find(preambel + rpck + suffix) != latexPackageList->end()) || (latexPackageList->find(preambel + rpck) != latexPackageList->end())) {
else if ( (latexPackageList->find(preambel + rpck + suffix) != latexPackageList->end())
|| (latexPackageList->find(preambel + rpck) != latexPackageList->end())
|| localPackage) {
dlh->addOverlay(QFormatRange(tk.start, tk.length, packagePresentFormat));
} else {
dlh->addOverlay(QFormatRange(tk.start, tk.length, packageMissingFormat));
Expand Down Expand Up @@ -2718,7 +2738,9 @@ void LatexEditorView::mouseHovered(QPoint pos)
}
QString text = QString("%1:&nbsp;<b>%2</b>").arg(type,value);
const QString suffix = tk.type == Token::documentclass ? ".cls" : ".sty";
if (latexPackageList->find(preambel + value + suffix) != latexPackageList->end() || latexPackageList->find(preambel + value) != latexPackageList->end()) {
if (latexPackageList->find(preambel + value + suffix) != latexPackageList->end()
|| latexPackageList->find(preambel + value) != latexPackageList->end()
|| value.startsWith(".")) { // don't check relative paths,i.e. local packages
QString description = LatexRepository::instance()->shortDescription(value);
if (!description.isEmpty()) text += "<br>" + description;
QToolTip::showText(editor->mapToGlobal(editor->mapFromFrame(pos)), text);
Expand Down
3 changes: 3 additions & 0 deletions src/latexpackage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ LatexPackage loadCwlFile(const QString fileName, LatexCompleterConfig *config, Q
LatexPackage package;

QFile tagsfile("cwl:" + fileName);
if(QFileInfo(fileName).isAbsolute() && !tagsfile.exists()){
tagsfile.setFileName(fileName);
}
bool skipSection = false;
if (tagsfile.exists() && tagsfile.open(QFile::ReadOnly)) {
QString line;
Expand Down

0 comments on commit c7eb058

Please sign in to comment.