-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Checkboxes and summaries #129
Open
aboukirev
wants to merge
30
commits into
vscode-org-mode:develop
Choose a base branch
from
aboukirev:feature/checkboxes
base: develop
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
8e80df9
Readme Buttons (#1)
sipi 2bd934a
Brought in checkboxes extension as a feature with initial test struct…
aboukirev 0d76b67
Make summary update test pass.
aboukirev 4618130
Added checkbox toggle test.
aboukirev 36554f8
Make ticking checkbox test pass.
aboukirev c3937a6
Added test ticking parent checkbox ticks all children
aboukirev 4e4e0a9
Added test for unticking last ticked child checkbox
aboukirev d17fbfd
Shortcicuit checking for parent nodes whenever possible
aboukirev b98f434
Added test for ticking all child checkboxes
aboukirev a394e28
Use undefined instead of null
aboukirev 80aed16
Added tabs to spaces conversion with tests
aboukirev 5143acd
One more null to undefined replacement
aboukirev de0030a
Refactor movement and selection in tests
aboukirev 3e13344
Parameterized tab expansion function with optional tab size
aboukirev 62f3111
Use string content instead of fixture in tests
aboukirev f379252
Refactor tests for correctness
aboukirev fb4a400
Added testing for expansion of 8-space tabs
aboukirev 93cc884
Use async instead of promises in tests
aboukirev 9245516
Added 3-level checkbox propagation test
aboukirev 0289813
Fixes formatting, braces, comments, parameter names
aboukirev b3399a6
Replace null with undefined in comment
aboukirev 8dd8880
Update for consistency
aboukirev dfb9106
Integrate checkboxes features with the extension
aboukirev 5741818
Remove command registration from tests
aboukirev 9a9c3b4
Added test for ticking parent and unticking one child
aboukirev c608c7e
Get document tab witdh from workspace configuration
aboukirev e606ced
Added key bindings for summary update and checkbox toggle
aboukirev 729520b
Comply with camelCase standard for exported functions
aboukirev b8f9ea1
Updated keybinding and provided command contribution info for checkboxes
aboukirev 3cb2c68
Merge branch 'develop' into feature/checkboxes
aboukirev File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
'use strict'; | ||
|
||
import { window, workspace, TextLine, Range, TextEditor, TextEditorEdit } from 'vscode'; | ||
|
||
// Checkbox is represented by exactly one symbol between square brackets. Symbol indicates status: '-' undetermined, 'x' or 'X' checked, ' ' unchecked. | ||
const checkboxRegex = /\[([-xX ])\]/; | ||
// Summary is a cookie indicating the number of ticked checkboxes in the child list relative to the total number of checkboxes in the list. | ||
const summaryRegex = /\[(\d*\/\d*)\]/; | ||
// Percentage is a cookie indicating the percentage of ticked checkboxes in the child list relative to the total number of checkboxes in the list. | ||
const percentRegex = /\[(\d*)%\]/; | ||
const indentRegex = /^(\s*)\S/; | ||
let orgTabSize: number = 4; | ||
|
||
export function orgTabsToSpaces(tabs: string, tabSize: number = 4): number { | ||
if (!tabs) { | ||
return 0; | ||
} | ||
let off = 1; | ||
for (let i = 0; i < tabs.length; i++) { | ||
if (tabs[i] == '\t') { | ||
off += tabSize - off % tabSize; | ||
} else { | ||
off++; | ||
} | ||
} | ||
return off; | ||
} | ||
|
||
export function orgToggleCheckbox(editor: TextEditor, edit: TextEditorEdit) { | ||
let doc = editor.document; | ||
let line = doc.lineAt(editor.selection.active.line); | ||
let checkbox = orgFindCookie(checkboxRegex, line); | ||
if (checkbox) { | ||
orgTabSize = workspace.getConfiguration('editor', doc.uri).get('tabSize'); | ||
let text = doc.getText(checkbox).toLowerCase(); | ||
let delta = orgCascadeCheckbox(edit, checkbox, line, text == 'x' ? ' ' : 'x'); | ||
let parent = orgFindParent(editor, line); | ||
// Since the updates as a result of toggle have not happened yet in the editor, | ||
// counting checked children is going to use old value of the current checkbox. | ||
// Hence the delta adjustment. | ||
if (parent) { | ||
orgUpdateParent(editor, edit, parent, delta); | ||
} | ||
} | ||
} | ||
|
||
export function orgUpdateSummary(editor: TextEditor, edit: TextEditorEdit) { | ||
let doc = editor.document; | ||
let line = doc.lineAt(editor.selection.active.line); | ||
orgTabSize = workspace.getConfiguration('editor', doc.uri).get('tabSize'); | ||
orgUpdateParent(editor, edit, line, 0); | ||
} | ||
|
||
// Pattern elements, like ratio summary, percent summary, checkbox, of the orgmode document are called cookies. | ||
function orgFindCookie(cookie: RegExp, line: TextLine): Range | undefined { | ||
let match = cookie.exec(line.text); | ||
if (match) { | ||
return new Range(line.lineNumber, match.index + 1, line.lineNumber, match.index + 1 + match[1].length); | ||
} | ||
return undefined; | ||
} | ||
|
||
function orgTriStateToDelta(value: string): number { | ||
switch (value) { | ||
case 'x': return 1; | ||
case ' ': return -1; | ||
default: return 0; | ||
} | ||
} | ||
|
||
function orgGetTriState(checked, total: number): string { | ||
return checked == 0 ? ' ' : (checked == total ? 'x' : '-'); | ||
} | ||
|
||
// Calculate and return indentation level of the line. Used in traversing nested lists and locating parent item. | ||
function orgGetIndent(line: TextLine): number { | ||
let match = indentRegex.exec(line.text); | ||
if (match) { | ||
return orgTabsToSpaces(match[1], orgTabSize); | ||
} | ||
return 0; | ||
} | ||
|
||
// Set checkbox to the desired state and perform necessary updates to child and parent elements (however many levels). | ||
function orgCascadeCheckbox(edit: TextEditorEdit, checkbox: Range, line: TextLine, state: string): number { | ||
if (!checkbox) { | ||
return 0; | ||
} | ||
let editor = window.activeTextEditor; | ||
let text = editor.document.getText(checkbox).toLowerCase(); | ||
if (text == state) { | ||
return 0; // Nothing to do. | ||
} | ||
edit.replace(checkbox, state); | ||
if (!line) { | ||
return orgTriStateToDelta(state); | ||
} | ||
let children = orgFindChildren(editor, line); | ||
let child: TextLine = undefined; | ||
for (child of children) { | ||
orgCascadeCheckbox(edit, orgFindCookie(checkboxRegex, child), child, state); | ||
} | ||
// If there is a summary cookie on this line, update it to either [0/0] or [total/total] depending on target state. | ||
let total = state ? children.length : 0; | ||
let summary = orgFindCookie(summaryRegex, line); | ||
if (summary) { | ||
edit.replace(summary, total.toString() + '/' + total.toString()); | ||
} | ||
// If there is a percent cookie on this line, update it to either [0%] or [100%] depending on target state. | ||
let percent = orgFindCookie(percentRegex, line); | ||
if (percent) { | ||
total = state == 'x' ? 100 : 0; | ||
edit.replace(percent, total.toString()); | ||
} | ||
return orgTriStateToDelta(state); | ||
} | ||
|
||
// Find parent item by walking lines up to the start of the file looking for a smaller indentation. | ||
// Does not ignore blank lines (indentation 0). | ||
function orgFindParent(editor: TextEditor, line: TextLine): TextLine | undefined { | ||
let doc = editor.document; | ||
let lnum = line.lineNumber; | ||
let indent = orgGetIndent(line); | ||
let parent = undefined; | ||
let pindent = indent; | ||
while (pindent >= indent) { | ||
lnum--; | ||
if (lnum < 0) { | ||
return undefined; | ||
} | ||
|
||
parent = doc.lineAt(lnum); | ||
pindent = orgGetIndent(parent); | ||
} | ||
return parent; | ||
} | ||
|
||
// Update checkbox and summary on this line. Adjust checked items count with an additional offset. | ||
// That accounts for a checkbox that has just been toggled but text in the editor has not been updated yet. | ||
function orgUpdateParent(editor: TextEditor, edit: TextEditorEdit, line: TextLine, adjust: number) { | ||
if (!line) { | ||
return; | ||
} | ||
let children = orgFindChildren(editor, line); | ||
let total = children.length; | ||
let checked = adjust; | ||
let chk: Range = undefined; | ||
let doc = editor.document; | ||
for (let child of children) { | ||
chk = orgFindCookie(checkboxRegex, child); | ||
if (doc.getText(chk).toLowerCase() == 'x') { | ||
checked++; | ||
} | ||
} | ||
let summary = orgFindCookie(summaryRegex, line); | ||
if (summary) { | ||
edit.replace(summary, checked.toString() + '/' + total.toString()); | ||
} | ||
let percent = orgFindCookie(percentRegex, line); | ||
if (percent) { | ||
edit.replace(percent, total == 0 ? '0' : (checked * 100 / total).toString()); | ||
} | ||
// If there is a checkbox on this line, update it depending on (checked == total). | ||
chk = orgFindCookie(checkboxRegex, line); | ||
// Prevent propagation downstream by passing line = undefined. | ||
let delta = orgCascadeCheckbox(edit, chk, undefined, orgGetTriState(checked, total)); | ||
// Recursively update parent nodes | ||
let parent = orgFindParent(editor, line); | ||
// Since the updates as a result of toggle have not happened yet in the editor, | ||
// counting checked children is going to use old value of the current checkbox. | ||
// Hence the delta adjustment. | ||
if (parent) { | ||
orgUpdateParent(editor, edit, parent, delta); | ||
} | ||
} | ||
|
||
// Find parent item by walking lines up to the start of the file looking for a smaller indentation. | ||
// Does not ignore blank lines (indentation 0). | ||
function orgFindChildren(editor: TextEditor, line: TextLine): TextLine[] { | ||
let children: TextLine[] = []; | ||
let lnum = line.lineNumber; | ||
let doc = editor.document; | ||
let lmax = doc.lineCount - 1; | ||
let indent = orgGetIndent(line); | ||
let child: TextLine = undefined; | ||
let cindent = indent; | ||
let next_indent = -1; | ||
while (lnum < lmax) { | ||
lnum++; | ||
child = doc.lineAt(lnum); | ||
cindent = orgGetIndent(child); | ||
if (cindent <= indent) { | ||
break; | ||
} | ||
if (next_indent < 0) { | ||
next_indent = cindent; | ||
} | ||
if (cindent <= next_indent) { | ||
children.push(child); | ||
} | ||
} | ||
return children; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to store
orgTabSize
globally. It can be changed on fly. And potentially break traversing via trees. I think it potentially can have corner-cases here. If I understand your logic correctly (sorry if I'm wrong, just made fast review yet and not JS developer).