Skip to content

Commit

Permalink
Add smart completions
Browse files Browse the repository at this point in the history
  • Loading branch information
jotaen committed Mar 26, 2022
1 parent 2bab64e commit 405003c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# klog for Sublime Text

This packages provides syntax-highlighting and some sensible default settings for working with [klog](https://klog.jotaen.net) files.
This packages provides support for working with [klog time tracking](https://klog.jotaen.net) files in the Sublime Text editor:

- Syntax highlighting
+ If you use the Mariana or Monokai theme, it also assigns meaningful colouring.
- Smart completion for inserting the current date or time. For example:
+ Typing `today` completes to the current date
+ Typing `now` completes to the current time
- A few sensible default settings for the `.klg` file format

![A klog file with sample data](resources/example.png)
5 changes: 0 additions & 5 deletions klog.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,4 @@

// Per specification, klog files must be UTF-8.
"default_encoding": "UTF-8",

// Tab completion doesn’t make sense for a data format,
// and it makes text-aligning harder.
"tab_completion": false,
"auto_complete": false,
}
4 changes: 2 additions & 2 deletions resources/test.klg
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ This record is indented with 2 spaces.
4h <- This is an entry value.
4h <- this is continues summary text.

2022-01-01
2022-01-01
^^^^^^^^^^ This is summary, not a date.

Expand All @@ -41,8 +40,9 @@ This record is indented with 2 spaces.

1234/12/31 (+3h!)

1999/01/01a Illegal: Headline cannot contain arbitrary text.
1999/01/01 Unrecognised: Headline cannot contain arbitrary text.
a Illegal: record summary lines cannot start with whitespace.
5h Illegal: First indentation must be once.
4hfoo Illegal: no space between value and summary.
asdf 4h Unrecognised, if the value is invalid. (This is not marked as illegal, though.)
a Illegal: cannot have un-indented line after entries.
58 changes: 58 additions & 0 deletions smart_completions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import sublime
import sublime_plugin
import datetime
import re


INDENT_PATTERN = re.compile('^( {2,4}|\t)')
NOT_INDENTED_PATTERN = re.compile('^[^\s]$')


def complete(trigger, value, details):
return sublime.CompletionItem(
trigger,
annotation=value,
completion=value,
completion_format=sublime.COMPLETION_FORMAT_SNIPPET,
kind=(sublime.KIND_ID_AMBIGUOUS, '⏱', 'klog'),
details=details,
)


def date_completions():
DATEFMT = '%Y-%m-%d'
today = datetime.datetime.now()
yesterday = today - datetime.timedelta(days=1)
tomorrow = today + datetime.timedelta(days=1)

return sublime.CompletionList([
complete('today', today.strftime(DATEFMT), 'Insert today’s date'),
complete('yesterday', yesterday.strftime(DATEFMT), 'Insert yesterday’s date'),
complete('tomorrow', tomorrow.strftime(DATEFMT), 'Insert tomorrow’s date'),
])


def time_completions():
TIMEFMT = '%H:%M'
now = datetime.datetime.now()
return sublime.CompletionList([
complete('now', now.strftime(TIMEFMT), 'Insert current time'),
complete('start', now.strftime(TIMEFMT) + ' - ?', 'Insert open range'),
])


class KlogEventListener(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
if not view.match_selector(locations[0], 'source.klog'):
return

cursor = view.line(view.sel()[0])
preceding_text = view.substr(cursor)

# If the cursor is not indented
if NOT_INDENTED_PATTERN.match(preceding_text):
return date_completions()

# If the cursor is on an indented line, offer times.
if INDENT_PATTERN.match(preceding_text):
return time_completions()

0 comments on commit 405003c

Please sign in to comment.