Commit 0851c9d3 authored by Ilia Kats's avatar Ilia Kats Committed by Christoph Cullmann
Browse files

improve completion scraping

- handle multiple LaTeX commands for the same symbol
- handle non-letters in LaTeX commands (e.g. for superscript/subscript)
parent f5a1cc55
...@@ -15,9 +15,6 @@ ...@@ -15,9 +15,6 @@
#include <KTextEditor/Document> #include <KTextEditor/Document>
#include <KTextEditor/View> #include <KTextEditor/View>
static const QRegularExpression latexexpr(QStringLiteral("\\\\:?[\\w]+:?$"),
QRegularExpression::DontCaptureOption); // no unicode here, LaTeX expressions are ASCII only
LatexCompletionModel::LatexCompletionModel(QObject *parent) LatexCompletionModel::LatexCompletionModel(QObject *parent)
: KTextEditor::CodeCompletionModel(parent) : KTextEditor::CodeCompletionModel(parent)
{ {
......
...@@ -10,6 +10,7 @@ OUTFNAME = "completiontrie.h" ...@@ -10,6 +10,7 @@ OUTFNAME = "completiontrie.h"
from urllib import request from urllib import request
from html.parser import HTMLParser from html.parser import HTMLParser
from string import ascii_letters, digits
class JuliaUnicodeCompletionsParser(HTMLParser): class JuliaUnicodeCompletionsParser(HTMLParser):
def __init__(self): def __init__(self):
...@@ -55,7 +56,9 @@ class JuliaUnicodeCompletionsParser(HTMLParser): ...@@ -55,7 +56,9 @@ class JuliaUnicodeCompletionsParser(HTMLParser):
return return
if self._in_body: if self._in_body:
if tag == "tr": if tag == "tr":
self.table.append(tuple(self._current_row)) for ccompletion in self._current_row[2].split(","):
self._current_row[2] = ccompletion.strip()
self.table.append(tuple(self._current_row))
self._current_row = [] self._current_row = []
elif tag == "table": elif tag == "table":
self._finished = True self._finished = True
...@@ -66,10 +69,14 @@ with request.urlopen(JULIA_UNICODE_DOCUMENTATION_URL) as page: ...@@ -66,10 +69,14 @@ with request.urlopen(JULIA_UNICODE_DOCUMENTATION_URL) as page:
parser.close() parser.close()
parser.table.sort(key=lambda x: x[2]) parser.table.sort(key=lambda x: x[2])
completionchars = set()
wordchars = set(list(ascii_letters) + list(digits) + ["_"])
with open(OUTFNAME, "w") as out: with open(OUTFNAME, "w") as out:
out.write("""\ out.write("""\
#include <tsl/htrie_map.h> #include <tsl/htrie_map.h>
#include <QString> #include <QString>
#include <QRegularExpression>
struct Completion { struct Completion {
QString codepoint; QString codepoint;
QString chars; QString chars;
...@@ -80,6 +87,9 @@ static const tsl::htrie_map<char, Completion> completiontrie({ ...@@ -80,6 +87,9 @@ static const tsl::htrie_map<char, Completion> completiontrie({
""") """)
for i, completion in enumerate(parser.table): for i, completion in enumerate(parser.table):
for letter in completion[2][1:]:
if letter not in wordchars:
completionchars.add(letter)
latexsym = completion[2].replace("\\", "\\\\") latexsym = completion[2].replace("\\", "\\\\")
if i > 0: if i > 0:
out.write(",") out.write(",")
...@@ -90,3 +100,16 @@ static const tsl::htrie_map<char, Completion> completiontrie({ ...@@ -90,3 +100,16 @@ static const tsl::htrie_map<char, Completion> completiontrie({
out.write("""\ out.write("""\
}); });
""") """)
have_dash = False
if "-" in completionchars:
have_dash = True
completionchars.discard("-")
if "]" in completionchars:
completionchars.discard("]")
completionchars.add("\\]")
charclass = "".join(completionchars)
if have_dash:
charclass += "-"
out.write(f'static const QRegularExpression latexexpr(QStringLiteral("\\\\\\\\:?[\\\\w{charclass}]+:?$"), QRegularExpression::DontCaptureOption);\n')
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment