|
1 | | -module.exports = { isPackageOrClass, isEnvDelim, isRef }; |
| 1 | +const { Range } = require("atom"); |
2 | 2 |
|
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)) { |
6 | 27 | return true; |
| 28 | + } else { |
| 29 | + return context.scopes.includes("support.class.latex"); // probably more consistent |
7 | 30 | } |
| 31 | +} |
8 | 32 |
|
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 | + } |
14 | 43 | } |
15 | 44 |
|
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; |
18 | 58 | } |
19 | 59 |
|
| 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; |
20 | 80 |
|
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 |
24 | 83 |
|
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; } |
29 | 85 |
|
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); |
31 | 88 |
|
32 | 89 | return { |
33 | | - value: startRef[1] + endRef[1], |
34 | | - refStartIndex, |
35 | | - refEndIndex |
| 90 | + value, |
| 91 | + range |
36 | 92 | }; |
37 | 93 | } |
0 commit comments