diff --git a/lib/main.js b/lib/main.js index 45bb5b5..ae001ba 100644 --- a/lib/main.js +++ b/lib/main.js @@ -21,11 +21,6 @@ module.exports = { this.disposables = new CompositeDisposable(); this.observedEditors = []; this.disposables.add( - - atom.config.observe("latex-folding.allowSameLineFolds", (value) => { - - }), - atom.workspace.observeTextEditors((editor) => { editorInfo.set(editor.id, { @@ -35,15 +30,11 @@ module.exports = { hooked: false }); - function mouseDown(ev) { - return toggleFold(ev, editor); - } - editor.observeGrammar((grammar) => { if (grammar.scopeName === "text.tex.latex") { - addFoldingRules(editor, mouseDown); + addFoldingRules(editor); } else { - removeFoldingRules(editor, mouseDown); + removeFoldingRules(editor); } }); }) @@ -58,7 +49,6 @@ module.exports = { }; function isFoldableAtRow (row, editor) { - if (row === 0) debugger; const line = editor.lineTextForBufferRow(row); return FOLD_POSITIONS.test(line); } @@ -76,13 +66,16 @@ function getFoldableRangeContainingPoint (point, editor) { if (match === null) return null; const foldCommand = match[1]; - return foldCommand === "begin" ? getEnvRange(editor, row) : getSectionRange(editor, row, foldCommand); + const range = foldCommand === "begin" ? getEnvRange(editor, row) : getSectionRange(editor, row, foldCommand); + + return range.start.row === range.end.row ? null : range; } -function addFoldingRules(editor, mouseDown) { +function addFoldingRules(editor) { let context = editorInfo.get(editor.id); - if (context.hooked) { console.warn("latex-folding: already hooked"); return; } + if (context.hooked) return; + context.hooked = true; const languageMode = editor.languageMode; @@ -96,7 +89,7 @@ function addFoldingRules(editor, mouseDown) { languageMode.getFoldableRangeContainingPoint = (point, tabLength) => getFoldableRangeContainingPoint(point, editor); } -function removeFoldingRules(editor, mouseDown) { +function removeFoldingRules(editor) { let context = editorInfo.get(editor.id); if (context && context.hooked) { context.hooked = false; @@ -135,7 +128,7 @@ function getSectionRange(editor, row, sectionType) { }; let startLevel = levelTable[sectionType]; - let sectionRange; + let sectionRange = null; let startPoint = new Point(row, sectionMatch[0].length); let searchRegex = /(?:\\((?:sub){0,2}section|chapter|part|(?:sub)?paragraph)\s*\*?\s*(\[.*?\])?\{([^\}]*\})?)|(?:\\end\s*\{\s*document\s*\})/g; @@ -160,15 +153,20 @@ function getSectionRange(editor, row, sectionType) { }); if (!matchFound) { - sectionRange = new Range(startPoint, endPosition); - } else { - sectionRange = new Range(startPoint, nextSectionCommandRange.start); + return sectionRange = new Range(startPoint, endPosition); } + + sectionRange = new Range(startPoint, nextSectionCommandRange.start); + + if (sectionRange.start.row >= sectionRange.end.row - 1) return null; + + sectionRange.end = new Point(sectionRange.end.row - 1, Infinity) + return sectionRange; } -function getEnvRange(editor, row) { +function getEnvRange(editor, row, lenientEnvNames=false) { let line = editor.lineTextForBufferRow(row); let envMatch = line.match(/^[ \t]*\\begin\s*\{(.*?)\}/); if (envMatch === null) { @@ -180,8 +178,10 @@ function getEnvRange(editor, row) { const ENV_NAME = envMatch[1]; + const SEARCH_NAME = lenientEnvNames ? '.*?' : escape(ENV_NAME); + let startPoint = new Point(row, envMatch[0].length); - let searchRegex = new RegExp(`\\\\(begin|end)\\{${ENV_NAME}\\}`); + let searchRegex = new RegExp(`\\\\(begin|end)\\{${SEARCH_NAME}\\}`, 'g'); let scanRange = new Range(startPoint, endPosition); @@ -217,3 +217,8 @@ function isCommented(scopesArray) { } return false; } + + +function escape (text) { + return text.replace(/\W/, c => '\\' + c) +} diff --git a/package.json b/package.json index 1b07d25..112bf68 100644 --- a/package.json +++ b/package.json @@ -18,13 +18,9 @@ }, "activationHooks": [ "language-latex:grammar-used", - "language-latex2e:grammar-used" + "language-latex2e:grammar-used", + "text.tex.latex:root-scope-used" ], "configSchema": { - "allowSameLineFolds": { - "description": "Enable this to allow folds to start and end on the same line.", - "type": "boolean", - "default": false - } } }