Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Break up tab boundary by space not by word #16

Merged
merged 4 commits into from
Mar 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions lib/snippet-expansion.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
_ = require 'underscore-plus'
{Subscriber} = require 'emissary'
{Point, Range} = require 'atom'
Snippet = require './snippet'

module.exports =
class SnippetExpansion
Expand All @@ -9,9 +11,10 @@ class SnippetExpansion
tabStopMarkers: null
settingTabStop: false


constructor: (@snippet, @editor) ->
@editor.selectToBeginningOfWord()
startPosition = @editor.getCursorBufferPosition()
startPosition = @selectToBoundaryPosition()

@editor.transact =>
[newRange] = @editor.insertText(snippet.body, autoIndent: false)
if snippet.tabStops.length > 0
Expand All @@ -21,6 +24,21 @@ class SnippetExpansion
@editor.normalizeTabsInBufferRange(newRange)
@indentSubsequentLines(startPosition.row, snippet) if snippet.lineCount > 1

selectToBoundaryPosition: ->
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should really be inside of Cursor, something like
getPreviousBoundaryBuffer(boundary: Regex, breakOnStartOfLine: false)
Iterates characters previous to current cursor position until a boundary is been hit.

Because I need to use this again in another package, but I cannot make this happen. @kevinsawicki ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is getBeginningOfCurrentWordBufferPosition which does take a wordRegex option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely looked into that already and it didn't work as expected for some reason, here is another use case:

\t....*..\t...|

vertical bar is cursor. periods is a simple space character. \t is tab. I want to look backward until I find my first non-whitespace character. Depending on if that's an asterix or not I do different things.

I will look into getBeginningOfCurrentWordBufferPosition again because I definitely already did once before and I couldn't get it to work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I was able to use it in this PR: #18

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok I see how you're doing it. I must have done something wonky. the PR looks good to me btw +1

cursor = @editor.getCursor()
line = cursor.getCurrentBufferLine()
newColumn = cursor.getBufferColumn()
column = newColumn
row = cursor.getBufferRow()
while newColumn >= 0
break if Snippet.prefixBoundary.test line[newColumn - 1]
newColumn--
if newColumn < 0 then newColumn = 0
startPoint = new Point(row, newColumn)
endPoint = new Point(row, column)
@editor.setSelectedBufferRange new Range(startPoint, endPoint)
startPoint

cursorMoved: ({oldBufferPosition, newBufferPosition, textChanged}) ->
return if @settingTabStop or textChanged
oldTabStops = @tabStopsForBufferPosition(oldBufferPosition)
Expand Down
1 change: 1 addition & 0 deletions lib/snippet.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ _ = require 'underscore-plus'

module.exports =
class Snippet
@prefixBoundary: /\s/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be good to add " and ' to this regex for this CoffeeScript case that is currently broken:

"#<tab>

atom/language-coffee-script#7

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I'll just do this in a separate PR, nevermind.

name: null
prefix: null
body: null
Expand Down
14 changes: 12 additions & 2 deletions lib/snippets.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,24 @@ module.exports =
getBodyParser: ->
@bodyParser ?= require './snippet-body-parser'

getPrefixText: (cursor) ->
line = cursor.getCurrentBufferLine()
i = cursor.getBufferColumn() - 1
prefix = []
while i >= 0
break if Snippet.prefixBoundary.test line[i]
prefix.unshift line[i]
i--

prefix.join ''

enableSnippetsInEditor: (editorView) ->
editor = editorView.getEditor()
editorView.command 'snippets:expand', (e) =>
unless editor.getSelection().isEmpty()
e.abortKeyBinding()
return

prefix = editor.getCursor().getCurrentWordPrefix()
prefix = @getPrefixText editor.getCursor()
if snippet = atom.syntax.getProperty(editor.getCursorScopes(), "snippets.#{prefix}")
editor.transact ->
new SnippetExpansion(snippet, editor)
Expand Down
20 changes: 20 additions & 0 deletions spec/snippets-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ describe "Snippets extension", ->
prefix: "t1"
body: "this is a test"

"special chars":
prefix: "@unique"
body: "@unique see"

"tab stops":
prefix: "t2"
body: """
Expand Down Expand Up @@ -232,6 +236,22 @@ describe "Snippets extension", ->
editorView.trigger keydownEvent('tab', target: editorView[0])
expect(buffer.lineForRow(0)).toBe "first line"

describe "when text contains special characters", ->
it "should see the special character as part of the tab boundary", ->
editor.insertText("@unique")
expect(editor.getCursorScreenPosition()).toEqual [0, 7]

editorView.trigger keydownEvent('tab', target: editorView[0])
expect(buffer.lineForRow(0)).toBe "@unique seevar quicksort = function () {"
expect(editor.getCursorScreenPosition()).toEqual [0, 11]
it "should see the special character as part of the tab boundary and select only the prefix", ->
editor.insertText("a; @unique")
expect(editor.getCursorScreenPosition()).toEqual [0, 10]

editorView.trigger keydownEvent('tab', target: editorView[0])
expect(buffer.lineForRow(0)).toBe "a; @unique seevar quicksort = function () {"
expect(editor.getCursorScreenPosition()).toEqual [0, 14]

describe "snippet loading", ->
[configDirPath, packageWithSnippets, packageWithBrokenSnippets] = []

Expand Down