Skip to content

Commit

Permalink
can't wait any longer
Browse files Browse the repository at this point in the history
  • Loading branch information
Aerijo committed Nov 25, 2018
1 parent fb4a916 commit 39cb391
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
49 changes: 27 additions & 22 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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);
}
});
})
Expand All @@ -58,7 +49,6 @@ module.exports = {
};

function isFoldableAtRow (row, editor) {
if (row === 0) debugger;
const line = editor.lineTextForBufferRow(row);
return FOLD_POSITIONS.test(line);
}
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);

Expand Down Expand Up @@ -217,3 +217,8 @@ function isCommented(scopesArray) {
}
return false;
}


function escape (text) {
return text.replace(/\W/, c => '\\' + c)
}
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

0 comments on commit 39cb391

Please sign in to comment.