From 910ca47a61162b415ec8af54deb692447bfbf9d1 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 29 Oct 2012 12:22:29 -0700 Subject: [PATCH] Allow setting of invisibles on editor --- spec/app/editor-spec.coffee | 7 ++++--- spec/app/root-view-spec.coffee | 19 ++++++------------- src/app/editor.coffee | 14 ++++++++++---- src/app/root-view.coffee | 8 +++++--- src/app/token.coffee | 16 +++++++--------- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index c3f639634..34e9d3d53 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -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) diff --git a/spec/app/root-view-spec.coffee b/spec/app/root-view-spec.coffee index a3d4cdd52..e93e1b1a7 100644 --- a/spec/app/root-view-spec.coffee +++ b/spec/app/root-view-spec.coffee @@ -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 "▸" \ No newline at end of file + expect(rootView.getInvisibles().tab).toBe "▸" diff --git a/src/app/editor.coffee b/src/app/editor.coffee index d769ec122..2b029d041 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -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("
")
 
+    invisibles = @invisibles if @showInvisibles
+
     if screenLine.text == ''
       line.push(" ") 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("") if @showInvisibles
+    if invisibles?.eol
+      line.push("")
+
     line.push('
') line.join('') diff --git a/src/app/root-view.coffee b/src/app/root-view.coffee index a356a8265..f772d0a5a 100644 --- a/src/app/root-view.coffee +++ b/src/app/root-view.coffee @@ -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 diff --git a/src/app/token.coffee b/src/app/token.coffee index 4671f316d..3e0046852 100644 --- a/src/app/token.coffee +++ b/src/app/token.coffee @@ -40,7 +40,7 @@ class Token isTab: true ) - getValueAsHtml: ({showInvisibles, invisiblesMap, hasLeadingWhitespace, hasTrailingWhitespace})-> + getValueAsHtml: ({invisibles, hasLeadingWhitespace, hasTrailingWhitespace})-> html = @value .replace(/&/g, '&') .replace(/"/g, '"') @@ -48,17 +48,15 @@ class Token .replace(//g, '>') - if showInvisibles - if @isTab - tabChar = invisiblesMap?.tab - html = html.replace(/^./, "") - else - spaceChar = invisiblesMap?.space + if invisibles + if @isTab and invisibles.tab + html = html.replace(/^./, "") + else if invisibles.space if hasLeadingWhitespace html = html.replace /^[ ]+/, (match) -> - "" + "" if hasTrailingWhitespace html = html.replace /[ ]+$/, (match) -> - "" + "" html