Allow setting of invisibles on editor

This commit is contained in:
Kevin Sawicki
2012-10-29 12:22:29 -07:00
parent ff8055be49
commit 910ca47a61
5 changed files with 32 additions and 32 deletions

View File

@@ -1522,13 +1522,14 @@ describe "Editor", ->
describe "when editor.setShowInvisibles is called", ->
it "displays spaces as •, tabs as ▸ and newlines as ¬ when true", ->
editor.attachToDom()
editor.setInvisibles(rootView.getInvisibles())
editor.setText " a line with tabs\tand spaces "
expect(editor.showInvisibles).toBeFalsy()
expect(editor.find('.line').text()).toBe " a line with tabs and spaces "
expect(editor.renderedLines.find('.line').text()).toBe " a line with tabs and spaces "
editor.setShowInvisibles(true)
expect(editor.find('.line').text()).toBe "•a line with tabs▸ and spaces•¬"
expect(editor.renderedLines.find('.line').text()).toBe "•a line with tabs▸ and spaces•¬"
editor.setShowInvisibles(false)
expect(editor.find('.line').text()).toBe " a line with tabs and spaces "
expect(editor.renderedLines.find('.line').text()).toBe " a line with tabs and spaces "
it "displays newlines(¬) as their own token outside of the other tokens scope", ->
editor.setShowInvisibles(true)

View File

@@ -715,13 +715,7 @@ describe "RootView", ->
lowerRightEditor = rightEditor.splitDown()
expect(lowerRightEditor.find(".line:first").text()).toBe " "
describe 'setInvisiblesMap', ->
it 'sets the invisibles map to the supplied hash', ->
rootView.setInvisiblesMap
eol: "¬"
space: ""
tab: ""
describe 'setInvisibles', ->
it 'updates the display of all edit sessions with the new map', ->
rootView.height(200)
rootView.attachToDom()
@@ -729,7 +723,7 @@ describe "RootView", ->
rightEditor.setText(" \t ")
leftEditor = rightEditor.splitLeft()
rootView.setInvisiblesMap
rootView.setInvisibles
eol: ";"
space: "_"
tab: "tab"
@@ -737,14 +731,13 @@ describe "RootView", ->
rootView.trigger "window:toggle-invisibles"
expect(rightEditor.find(".line:first").text()).toBe "_tab _;"
expect(leftEditor.find(".line:first").text()).toBe "_tab _;"
# todo
describe 'getInvisiblesMap', ->
describe 'getInvisibles', ->
it 'contains an eol key with a value', ->
expect(rootView.getInvisiblesMap().eol).toBe "¬"
expect(rootView.getInvisibles().eol).toBe "¬"
it 'contains a space key with a value', ->
expect(rootView.getInvisiblesMap().space).toBe ""
expect(rootView.getInvisibles().space).toBe ""
it 'contains a tab key with a value', ->
expect(rootView.getInvisiblesMap().tab).toBe ""
expect(rootView.getInvisibles().tab).toBe ""

View File

@@ -271,6 +271,9 @@ class Editor extends View
@showInvisibles = showInvisibles
@renderLines()
setInvisibles: (@invisibles={}) ->
@renderLines()
setText: (text) -> @getBuffer().setText(text)
getText: -> @getBuffer().getText()
getPath: -> @getBuffer().getPath()
@@ -365,6 +368,7 @@ class Editor extends View
@calculateDimensions()
@hiddenInput.width(@charWidth)
@setSoftWrapColumn() if @activeEditSession.getSoftWrap()
@invisibles = @rootView()?.getInvisibles()
$(window).on "resize.editor#{@id}", =>
@updateRenderedLines()
@focus() if @isFocused
@@ -871,6 +875,8 @@ class Editor extends View
attributePairs.push "#{attributeName}=\"#{value}\"" for attributeName, value of lineAttributes
line.push("<pre #{attributePairs.join(' ')}>")
invisibles = @invisibles if @showInvisibles
if screenLine.text == ''
line.push("&nbsp;") unless @showInvisibles
else
@@ -880,8 +886,7 @@ class Editor extends View
for token in screenLine.tokens
updateScopeStack(token.scopes)
line.push(token.getValueAsHtml(
showInvisibles: @showInvisibles
invisiblesMap: @rootView()?.getInvisiblesMap()
invisibles: invisibles
hasLeadingWhitespace: position < firstNonWhitespacePosition
hasTrailingWhitespace: position + token.value.length > firstTrailingWhitespacePosition
))
@@ -889,8 +894,9 @@ class Editor extends View
position += token.value.length
popScope() while scopeStack.length > 0
eolChar = @rootView()?.getInvisiblesMap().eol
line.push("<span class='invisible'>#{eolChar}</span>") if @showInvisibles
if invisibles?.eol
line.push("<span class='invisible'>#{invisibles.eol}</span>")
line.push('</pre>')
line.join('')

View File

@@ -31,7 +31,7 @@ class RootView extends View
extensionStates: null
fontSize: 20
showInvisibles: false
invisiblesMap:
invisibles:
eol: "¬"
space: ""
tab: ""
@@ -234,8 +234,10 @@ class RootView extends View
getFontSize: -> @fontSize
setInvisiblesMap: (@invisiblesMap) ->
getInvisiblesMap: -> @invisiblesMap
setInvisibles: (@invisibles={}) ->
editor.setInvisibles(@invisibles) for editor in @getEditors()
getInvisibles: -> @invisibles
loadUserConfiguration: ->
try

View File

@@ -40,7 +40,7 @@ class Token
isTab: true
)
getValueAsHtml: ({showInvisibles, invisiblesMap, hasLeadingWhitespace, hasTrailingWhitespace})->
getValueAsHtml: ({invisibles, hasLeadingWhitespace, hasTrailingWhitespace})->
html = @value
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
@@ -48,17 +48,15 @@ class Token
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
if showInvisibles
if @isTab
tabChar = invisiblesMap?.tab
html = html.replace(/^./, "<span class='invisible'>#{tabChar}</span>")
else
spaceChar = invisiblesMap?.space
if invisibles
if @isTab and invisibles.tab
html = html.replace(/^./, "<span class='invisible'>#{invisibles.tab}</span>")
else if invisibles.space
if hasLeadingWhitespace
html = html.replace /^[ ]+/, (match) ->
"<span class='invisible'>#{match.replace(/./g, spaceChar)}</span>"
"<span class='invisible'>#{match.replace(/./g, invisibles.space)}</span>"
if hasTrailingWhitespace
html = html.replace /[ ]+$/, (match) ->
"<span class='invisible'>#{match.replace(/./g, spaceChar)}</span>"
"<span class='invisible'>#{match.replace(/./g, invisibles.space)}</span>"
html