This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
LSP snippets #288
Closed
Closed
LSP snippets #288
Changes from 76 commits
Commits
Show all changes
81 commits
Select commit
Hold shift + click to select a range
a1ff497
Start on LSP syntax parser
Aerijo bb03336
Update grammar for 95% compliance
Aerijo 08019c5
Ony escape necessary characters
Aerijo 675b7d4
return regex
Aerijo df90657
formatting and tweaks
Aerijo 2cf43b8
remove unused file
Aerijo bd5700b
Merge branch 'master' of github.com:Aerijo/snippets into lsp-snippets
Aerijo 8f022fc
Add variable support
Aerijo 1c42d1b
Hah, pass all specs
Aerijo 6cc9446
Pass editor and cursor to variable resolver
Aerijo 9d32670
remove debugger
Aerijo c60b3ad
Resolve all the things
Aerijo 4be086c
Fix selection contents & line numbers
Aerijo 4863ca6
remove debugger
Aerijo 139e08a
Apply (some) variable transforms
Aerijo fc7d227
Refrain from assigning to constant
Aerijo b9d967a
Take previous advice
Aerijo f9a2d72
...
Aerijo 7c5cc77
Resolve global replacement properly
Aerijo 5746276
Informative error when failing to transform variable
Aerijo 85107f4
Actually throw an error
Aerijo c9b2996
general idea
Aerijo 82625e4
Mockup choices presentation
Aerijo 31fe36e
don't save config change
Aerijo 3a29fd9
Refactor
Aerijo 443bd62
remove debugger and pass entire acc to variable resolver
Aerijo 49f7e05
Add default params fallback
Aerijo 6542e76
Add in implicit end stop by default
Aerijo e002906
Fix implicit end stops & catch transform errors
Aerijo 81dbaa0
rename driver service
Aerijo c64242e
:art:
Aerijo b797802
fix resolver service
Aerijo 75a0641
Consistent transform behaviour
Aerijo ce4d69e
Start messing with history grouping
Aerijo 417dd93
Hack in forced undo barrier
Aerijo bea05d9
extract to method
Aerijo b156bb2
Maybe this?
Aerijo 77e8bb1
Use checkpoint instead
Aerijo 30cd9fb
Try more stuff
Aerijo 7cb2c8f
Improvements
Aerijo b8730af
clean end tabstop logic
Aerijo 0544e51
also make it correct -_-
Aerijo 231e617
Support if/else syntax
Aerijo c86b630
Fix tabstop position on undo/redo
Aerijo 09d866d
comment the change
Aerijo cf1441f
Expand and pass around resolver classes
Aerijo b6bbe9f
remove debugger
Aerijo 80fb5ca
fix typo
Aerijo a6d94e9
Merge branch 'master' of https://github.com/atom/snippets into lsp-sn…
Aerijo 3c721d7
implicit returns strike again
Aerijo e7432ed
Style & logic tweaks
Aerijo dae7f48
use single quotes
Aerijo df13d06
but not for JSON files
Aerijo 0d8d640
translate snippet tests
Aerijo ba1bc74
Don't load language-javascript
Aerijo cea1151
vaguely better undo/redo behaviour
Aerijo c77cf00
fix
Aerijo 01db360
Remove debugger & tweak gotoPreviousStop
Aerijo ac06db5
bug fixes
Aerijo b847db1
Specs and bug fixes
Aerijo 1cb4511
Test misc cases
Aerijo 7934443
remove console log
Aerijo 429a127
start testing snippet body text
Aerijo 5f20163
relax undefined check
Aerijo 43bae26
Make tests more robust and flexible
Aerijo 260d437
more specs
Aerijo 60f3af0
:art:
Aerijo 687b174
fix undo specs
Aerijo fb4b3e9
Fix yet another undo bug
Aerijo be3d800
extract transaction logic
Aerijo 9a0e477
don't add implicit endTabStop if already ending with tabstop
Aerijo d812cf0
Bring the changes from #281 into #288.
savetheclocktower 70ef2f9
remove unused import
Aerijo 77eaef8
Merge branch 'master' of github.com:atom/snippets into lsp-snippets
Aerijo 417da46
add back two removed tests
Aerijo 7f05767
remove unused imports
Aerijo 874aafa
move bundled snippets to the conventional folder
Aerijo 00ffa45
Merge branch 'lsp-snippets' of https://github.com/Aerijo/snippets int…
Aerijo 64606bc
alternate if-else syntax
Aerijo 6222c42
reorder alternate
Aerijo 0deaa1d
adjust spec
Aerijo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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,223 @@ | ||
const path = require('path') | ||
|
||
|
||
class ValueResolver { | ||
constructor (resolvers = new Map) { | ||
this.resolvers = resolvers | ||
} | ||
|
||
add (varName, resolver) { | ||
this.resolvers.set(varName, resolver) | ||
} | ||
|
||
/* | ||
Params depend on context. VariableResolver can expect the following, but TransformResolver will likely get a restricted number | ||
(VariableResolver) params = { | ||
variable: the variable name this was called with | ||
editor: the TextEditor we are expanding in | ||
cursor: the cursor we are expanding from | ||
indent: the indent of the original cursor line. Automatically applied to all text (post variable transformation) | ||
selectionRange: the original selection range of the cursor. This has been modified on the actual cursor to now select the prefix | ||
startPosition: the cursor selection start position, after being adjusted to select the prefix | ||
row: the row the start of the variable will be inserted on (final) <- final for snippet body creation. Does not account for changes when the user starts typing | ||
column: the column the start of the variable will be inserted on (final; accounts for indent) | ||
} | ||
|
||
(TransformResolver) params = { | ||
input: the text to be transformed | ||
transform: the transform this was called with | ||
} | ||
*/ | ||
resolve (name, params) { | ||
const resolver = this.resolvers.get(name) | ||
if (resolver) { | ||
return { | ||
hasResolver: true, | ||
value: resolver(params) | ||
} | ||
} | ||
return { | ||
hasResolver: false, | ||
value: undefined | ||
} | ||
} | ||
} | ||
|
||
class VariableResolver extends ValueResolver { | ||
constructor (resolvers = new Map) { | ||
super(new Map([ | ||
['CLIPBOARD', resolveClipboard], | ||
|
||
['TM_SELECTED_TEXT', resolveSelected], | ||
['TM_CURRENT_LINE', resolveCurrentLine], | ||
['TM_CURRENT_WORD', resolveCurrentWord], | ||
['TM_LINE_INDEX', resolveLineIndex], | ||
['TM_LINE_NUMBER', resolveLineNumber], | ||
['TM_FILENAME', resolveFileName], | ||
['TM_FILENAME_BASE', resolveFileNameBase], | ||
['TM_DIRECTORY', resolveFileDirectory], | ||
['TM_FILEPATH', resolveFilePath], | ||
|
||
['CURRENT_YEAR', resolveYear], | ||
['CURRENT_YEAR_SHORT', resolveYearShort], | ||
['CURRENT_MONTH', resolveMonth], | ||
['CURRENT_MONTH_NAME', resolveMonthName], | ||
['CURRENT_MONTH_NAME_SHORT', resolveMonthNameShort], | ||
['CURRENT_DATE', resolveDate], | ||
['CURRENT_DAY_NAME', resolveDayName], | ||
['CURRENT_DAY_NAME_SHORT', resolveDayNameShort], | ||
['CURRENT_HOUR', resolveHour], | ||
['CURRENT_MINUTE', resolveMinute], | ||
['CURRENT_SECOND', resolveSecond], | ||
|
||
['BLOCK_COMMENT_START', resolveBlockCommentStart], | ||
['BLOCK_COMMENT_END', resolveBlockCommentEnd], | ||
['LINE_COMMENT', resolveLineComment], | ||
|
||
...resolvers | ||
])) | ||
} | ||
} | ||
|
||
function resolveClipboard () { | ||
return atom.clipboard.read() | ||
} | ||
|
||
function resolveSelected ({editor, selectionRange}) { | ||
if (!selectionRange || selectionRange.isEmpty()) return undefined | ||
return editor.getTextInBufferRange(selectionRange) | ||
} | ||
|
||
function resolveCurrentLine ({editor, cursor}) { | ||
return editor.lineTextForBufferRow(cursor.getBufferRow()) | ||
} | ||
|
||
function resolveCurrentWord ({editor, cursor}) { | ||
return editor.getTextInBufferRange(cursor.getCurrentWordBufferRange()) | ||
} | ||
|
||
function resolveLineIndex ({cursor}) { | ||
return `${cursor.getBufferRow()}` | ||
} | ||
|
||
function resolveLineNumber ({cursor}) { | ||
return `${cursor.getBufferRow() + 1}` | ||
} | ||
|
||
function resolveFileName ({editor}) { | ||
return editor.getTitle() | ||
} | ||
|
||
function resolveFileNameBase ({editor}) { | ||
const fileName = resolveFileName({editor}) | ||
if (!fileName) { return undefined } | ||
|
||
const index = fileName.lastIndexOf('.') | ||
if (index >= 0) { | ||
return fileName.slice(0, index) | ||
} | ||
|
||
return fileName | ||
} | ||
|
||
function resolveFileDirectory ({editor}) { | ||
const filePath = editor.getPath() | ||
if (filePath === undefined) return undefined | ||
return path.dirname(filePath) | ||
} | ||
|
||
function resolveFilePath ({editor}) { | ||
return editor.getPath() | ||
} | ||
|
||
|
||
// TODO: Use correct locale | ||
function resolveYear () { | ||
return new Date().toLocaleString('en-us', {year: 'numeric'}) | ||
} | ||
|
||
function resolveYearShort () { // last two digits of year | ||
return new Date().toLocaleString('en-us', {year: '2-digit'}) | ||
} | ||
|
||
function resolveMonth () { | ||
return new Date().toLocaleString('en-us', {month: '2-digit'}) | ||
} | ||
|
||
function resolveMonthName () { | ||
return new Date().toLocaleString('en-us', {month: 'long'}) | ||
} | ||
|
||
function resolveMonthNameShort () { | ||
return new Date().toLocaleString('en-us', {month: 'short'}) | ||
} | ||
|
||
function resolveDate () { | ||
return new Date().toLocaleString('en-us', {day: '2-digit'}) | ||
} | ||
|
||
function resolveDayName () { | ||
return new Date().toLocaleString('en-us', {weekday: 'long'}) | ||
} | ||
|
||
function resolveDayNameShort () { | ||
return new Date().toLocaleString('en-us', {weekday: 'short'}) | ||
} | ||
|
||
function resolveHour () { | ||
return new Date().toLocaleString('en-us', {hour12: false, hour: '2-digit'}) | ||
} | ||
|
||
function resolveMinute () { | ||
return new Date().toLocaleString('en-us', {minute: '2-digit'}) | ||
} | ||
|
||
function resolveSecond () { | ||
return new Date().toLocaleString('en-us', {second: '2-digit'}) | ||
} | ||
|
||
// TODO: wait for https://github.com/atom/atom/issues/18812 | ||
// Could make a start with what we have; one of the two should be available | ||
function getEditorCommentStringsForPoint (_editor, _point) { | ||
return {line: '//', start: '/*', end: '*/'} | ||
} | ||
|
||
function resolveBlockCommentStart ({editor, cursor}) { | ||
const delims = getEditorCommentStringsForPoint(editor, cursor.getBufferPosition()) | ||
return delims.start | ||
} | ||
|
||
function resolveBlockCommentEnd ({editor, cursor}) { | ||
const delims = getEditorCommentStringsForPoint(editor, cursor.getBufferPosition()) | ||
return delims.end | ||
} | ||
|
||
function resolveLineComment ({editor, cursor}) { | ||
const delims = getEditorCommentStringsForPoint(editor, cursor.getBufferPosition()) | ||
return delims.line | ||
} | ||
|
||
class TransformResolver extends ValueResolver { | ||
constructor (resolvers = new Map) { | ||
super(new Map([ | ||
['upcase', transformUpcase], | ||
['downcase', transformDowncase], | ||
['capitalize', transformCapitalize], | ||
...resolvers | ||
])) | ||
} | ||
} | ||
|
||
function transformUpcase ({input}) { | ||
return input.toLocaleUpperCase() | ||
} | ||
|
||
function transformDowncase ({input}) { | ||
return input.toLocaleLowerCase() | ||
} | ||
|
||
function transformCapitalize ({input}) { | ||
return input ? input[0].toLocaleUpperCase() + input.substr(1) : '' | ||
} | ||
|
||
module.exports = { ValueResolver, VariableResolver, TransformResolver } |
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.
What is VSCode's behavior here? I can't think of anyplace in Atom that envisions non-English locales, but I'm curious about what would need to happen for this
TODO
to be done.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.
Really, I have no idea how any locale stuff works. So that was to make sure any final result is intentional, not accidental