mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Subsequent lines of a snippet are indented based on the first line
This commit is contained in:
@@ -22,11 +22,17 @@ describe "Snippets extension", ->
|
||||
this is a test
|
||||
endsnippet
|
||||
|
||||
snippet t2 "Snippet with tab stops"
|
||||
snippet t2 "With tab stops"
|
||||
go here next:($2) and finally go here:($3)
|
||||
go here first:($1)
|
||||
|
||||
endsnippet
|
||||
|
||||
snippet t3 "With indented second line"
|
||||
line 1
|
||||
line 2
|
||||
|
||||
endsnippet
|
||||
"""
|
||||
|
||||
describe "when the letters preceding the cursor trigger a snippet", ->
|
||||
@@ -75,6 +81,14 @@ describe "Snippets extension", ->
|
||||
expect(buffer.lineForRow(2)).toBe "go here next:(abc) and finally go here:( )"
|
||||
expect(editor.activeEditSession.getAnchors().length).toBe anchorCountBefore
|
||||
|
||||
describe "when a the start of the snippet is indented", ->
|
||||
it "indents the subsequent lines of the snippet to be even with the start of the first line", ->
|
||||
editor.setCursorScreenPosition([2, Infinity])
|
||||
editor.insertText ' t3'
|
||||
editor.trigger 'snippets:expand'
|
||||
expect(buffer.lineForRow(2)).toBe " if (items.length <= 1) return items; line 1"
|
||||
expect(buffer.lineForRow(3)).toBe " line 2"
|
||||
|
||||
describe "when the letters preceding the cursor don't match a snippet", ->
|
||||
it "inserts a tab as normal", ->
|
||||
editor.insertText("xte")
|
||||
|
||||
@@ -3,6 +3,10 @@ Point = require 'point'
|
||||
|
||||
module.exports =
|
||||
class Snippet
|
||||
body: null
|
||||
lineCount: null
|
||||
tabStops: null
|
||||
|
||||
constructor: ({@bodyPosition, @prefix, @description, body}) ->
|
||||
@body = @extractTabStops(body)
|
||||
|
||||
@@ -21,6 +25,7 @@ class Snippet
|
||||
column += segment.length
|
||||
bodyText.push(lineText.join(''))
|
||||
row++; column = 0
|
||||
@lineCount = row + 1
|
||||
|
||||
@tabStops = []
|
||||
for index in _.keys(tabStopsByIndex).sort()
|
||||
|
||||
@@ -45,18 +45,25 @@ class SnippetsSession
|
||||
prefix = @editSession.getLastCursor().getCurrentWordPrefix()
|
||||
if snippet = snippets[prefix]
|
||||
@editSession.selectToBeginningOfWord()
|
||||
snippetStartPosition = @editSession.getCursorBufferPosition()
|
||||
startPosition = @editSession.getCursorBufferPosition()
|
||||
@editSession.insertText(snippet.body)
|
||||
if snippet.tabStops.length
|
||||
@placeTabStopAnchors(snippetStartPosition, snippet.tabStops)
|
||||
@setTabStopIndex(0)
|
||||
@placeTabStopAnchors(startPosition, snippet.tabStops)
|
||||
@indentSnippet(startPosition.row, snippet)
|
||||
true
|
||||
else
|
||||
false
|
||||
|
||||
placeTabStopAnchors: (snippetStartPosition, tabStopPositions) ->
|
||||
placeTabStopAnchors: (startPosition, tabStopPositions) ->
|
||||
return unless tabStopPositions.length
|
||||
@tabStopAnchors = tabStopPositions.map (tabStopPosition) =>
|
||||
@editSession.addAnchorAtBufferPosition(snippetStartPosition.add(tabStopPosition))
|
||||
@editSession.addAnchorAtBufferPosition(startPosition.add(tabStopPosition))
|
||||
@setTabStopIndex(0)
|
||||
|
||||
indentSnippet: (startRow, snippet) ->
|
||||
if snippet.lineCount > 1
|
||||
initialIndent = @editSession.lineForBufferRow(startRow).match(/^\s*/)[0]
|
||||
for row in [startRow + 1...startRow + snippet.lineCount]
|
||||
@editSession.buffer.insert([row, 0], initialIndent)
|
||||
|
||||
goToNextTabStop: ->
|
||||
return false unless @tabStopAnchors
|
||||
|
||||
Reference in New Issue
Block a user