Skip to content

Commit 4790faa

Browse files
committed
Saving progress
1 parent fc3a7d3 commit 4790faa

File tree

5 files changed

+411
-179
lines changed

5 files changed

+411
-179
lines changed

lib/context-helpers.js

Lines changed: 78 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,93 @@
1-
module.exports = { isPackageOrClass, isEnvDelim, isRef };
1+
const { Range } = require("atom");
22

3-
function isPackageOrClass(editor, point, lineStart) {
4-
// if (lineStart.length > )
5-
if (lineStart.match(/\\(?:usepackage|documentclass)\s*(?:\[.*?\])?\{$/)) {
3+
module.exports = { isFilePath, isPackageOrClass, isEnvDelim, isRef, getBraceContents };
4+
5+
function isFilePath(editor, point, context) {
6+
/**
7+
* Notes on syntax of file path commands:
8+
* - \input only takes a single path; minimal def. is just file name.
9+
* - \include takes comma separated list of paths, just file name is also minimal requirement
10+
* - Other commands:
11+
* - \includeonly,
12+
* - \includegraphics,
13+
* - \inputminted,
14+
* - \addbibresource,
15+
* - !TeX root = NOTE: This one won't be picked up by the brace contents; maybe transfer to separate function?
16+
*/
17+
18+
19+
20+
}
21+
22+
23+
function isPackageOrClass(editor, point, context) {
24+
let lineStart = context.line.slice(0, context.index);
25+
let pkgRegex = /\\(?:usepackage|documentclass)\s*(?:\[.*?\])?\{[^\}]*$/;
26+
if (lineStart.match(pkgRegex)) {
627
return true;
28+
} else {
29+
return context.scopes.includes("support.class.latex"); // probably more consistent
730
}
31+
}
832

9-
// If the simple check doesn't work, try with scope (provided by grammar package) support.
10-
// will not work (or will lead to strange results) if the package does not follow `language-latex` conventions
11-
let scopes = editor.scopeDescriptorForBufferPosition(point).getScopesArray();
12-
let includes = scopes.includes("support.class.latex", 1); // first index is always root scope, so skip it
13-
return includes;
33+
function isEnvDelim(editor, point, context) {
34+
let line = context.line;
35+
let index = context.startIndex;
36+
let envRegex = /\\(begin|end)\s*\{[^\}]*$/;
37+
let env = line.slice(0, index).match(envRegex);
38+
if (env) {
39+
return env[1];
40+
} else {
41+
return false;
42+
}
1443
}
1544

16-
function isEnvDelim(lineStart) {
17-
return lineStart.match(/\\(?:begin|end)\s*\{$/);
45+
function isRef(editor, point, context) {
46+
let line = context.line;
47+
48+
let refRegex = /\\((?:auto|name|page|eq|cpage|c|labelc)?ref(?:\*)?)\s*\{[^\}]*$/i;
49+
let ref = line.slice(0, point.column).match(refRegex);
50+
if (ref) { let command = ref[1]; return command; }
51+
52+
let scopes = context.scopes;
53+
if (scopes.includes("meta.reference.latex")) {
54+
return "UNKNOWN REF COMMAND"; // need to find better way to handle this
55+
}
56+
57+
return false;
1858
}
1959

60+
/**
61+
* The given point represents the location of where the cursor
62+
* would go if the user clicked. The indexing is to the left
63+
* hand side of each character. I.e, if the line is
64+
*
65+
* >> "abc"
66+
*
67+
* - clicking to the left hand side of the "a" would make the
68+
* Point [row, 0], and the character at index 0 is the "a".
69+
*
70+
* - clicking to the right of the "a" is Point [row, 1]
71+
* and the character at that index is "b"
72+
*
73+
* TL;DR: I dislike fencepost off-by-one things. Each point
74+
* is a space between letters and is "asscoiated" with the letter
75+
* to it's right.
76+
*/
77+
function getBraceContents(editor, point, context) {
78+
let index = point.column;
79+
let line = context.line;
2080

21-
function isRef(line, lineIndex) {
22-
let startRef = line.slice(0, lineIndex).match(/\\(?:auto|name|page|eq|cpage|c|labelc)?ref(?:\*)?\{([^\}]*)$/i);
23-
if (!startRef) { return false; }
81+
let contentsStartIndex = line.lastIndexOf("{", index - 1); // if charAt == "{", then they clicked to the left of it and shouldn't match
82+
let contentsEndIndex = line.indexOf("}", index); // if charAt == "}", they clicked to left and it's fine
2483

25-
let endRef = line.slice(lineIndex).match(/^(.*?)\}/);
26-
if (!endRef) { return false; }
27-
let refStartIndex = startRef.index + startRef[0].match("{").index + 1;
28-
let refEndIndex = endRef[0].match("}").index + lineIndex;
84+
if (contentsStartIndex === -1 || contentsEndIndex === -1) { return null; }
2985

30-
// console.log(refStartIndex, refEndIndex, line.slice(refStartIndex, refEndIndex), line);
86+
let range = new Range([point.row, contentsStartIndex + 1], [point.row, contentsEndIndex]);
87+
let value = editor.getTextInBufferRange(range);
3188

3289
return {
33-
value: startRef[1] + endRef[1],
34-
refStartIndex,
35-
refEndIndex
90+
value,
91+
range
3692
};
3793
}

lib/document-parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class UniqueDocumentTree {
6969

7070
// Get all the file paths in the current file / node and store them as unsorted
7171
fs.readFile(currentPath, "utf8", (err, data) => {
72-
if (err) { console.error(err); return; }
72+
if (err) { console.error("Cannot read file:", err); return; }
7373
let match = searchTerm.exec(data);
7474
if (match) {
7575
node.tree.emitter.emit("endPatternInPath", currentPath);

0 commit comments

Comments
 (0)