Support using custom guide columns via config

This commit is contained in:
Kevin Sawicki
2012-12-28 08:44:57 -08:00
parent f24f95560e
commit 0aa8a65903
2 changed files with 29 additions and 39 deletions

View File

@@ -38,39 +38,23 @@ describe "WrapGuide", ->
expect(wrapGuide.position().left).toBeGreaterThan(initial)
expect(wrapGuide).toBeVisible()
describe "overriding getGuideColumn", ->
it "invokes the callback with the editor path", ->
editorPath = null
wrapGuide.getGuideColumn = (path) ->
editorPath = path
80
describe "using a custom config column", ->
it "places the wrap guide at the custom column", ->
config.set('wrapGuide.columns', [{pattern: '\.js$', column: 20}])
wrapGuide.updateGuide()
expect(editorPath).toBe(require.resolve('fixtures/sample.js'))
it "invokes the callback with a default value", ->
column = null
wrapGuide.getGuideColumn = (path, defaultColumn) ->
editorPath = path
column = defaultColumn
defaultColumn
width = editor.charWidth * 20
expect(width).toBeGreaterThan(0)
expect(wrapGuide.position().left).toBe(width)
it "uses the default column when no custom column matches the path", ->
config.set('wrapGuide.columns', [{pattern: '\.jsp$', column: '100'}])
wrapGuide.updateGuide()
expect(column).toBeGreaterThan(0)
width = editor.charWidth * wrapGuide.defaultColumn
expect(width).toBeGreaterThan(0)
expect(wrapGuide.position().left).toBe(width)
# this is disabled because we no longer support passing config to an extension
# at load time. we need to convert it to use the global config vars.
xit "uses the function from the config data", ->
rootView.find('.wrap-guide').remove()
config =
getGuideColumn: ->
1
atom.loadPackage('wrap-guide', config)
wrapGuide = rootView.find('.wrap-guide').view()
expect(wrapGuide.getGuideColumn).toBe(config.getGuideColumn)
it "hides the guide when the column is less than 1", ->
wrapGuide.getGuideColumn = (path) ->
-1
it "hides the guide when the config column is less than 1", ->
config.set('wrapGuide.columns', [{pattern: 'sample\.js$', column: -1}])
wrapGuide.updateGuide()
expect(wrapGuide).toBeHidden()

View File

@@ -1,21 +1,22 @@
{View} = require 'space-pen'
$ = require 'jquery'
_ = require 'underscore'
module.exports =
class WrapGuide extends View
@activate: (rootView, state, config) ->
@activate: (rootView, state) ->
requireStylesheet 'wrap-guide.css'
for editor in rootView.getEditors()
if rootView.parents('html').length
@appendToEditorPane(rootView, editor, config)
@appendToEditorPane(rootView, editor)
rootView.on 'editor-open', (e, editor) =>
@appendToEditorPane(rootView, editor, config)
@appendToEditorPane(rootView, editor)
@appendToEditorPane: (rootView, editor, config) ->
if underlayer = editor.pane()?.find('.underlayer')
underlayer.append(new WrapGuide(rootView, editor, config))
underlayer.append(new WrapGuide(rootView, editor))
@content: ->
@div class: 'wrap-guide'
@@ -23,17 +24,22 @@ class WrapGuide extends View
getGuideColumn: null
defaultColumn: 80
initialize: (@rootView, @editor, options = {}) =>
if typeof options.getGuideColumn is 'function'
@getGuideColumn = options.getGuideColumn
else
@getGuideColumn = (path, defaultColumn) -> defaultColumn
initialize: (@rootView, @editor) =>
@observeConfig 'editor.fontSize', => @updateGuide()
@subscribe @editor, 'editor-path-change', => @updateGuide()
@subscribe @editor, 'editor:min-width-changed', => @updateGuide()
@subscribe $(window), 'resize', => @updateGuide()
getGuideColumn: (path) ->
customColumns = config.get('wrapGuide.columns')
return @defaultColumn unless _.isArray(customColumns)
for customColumn in customColumns
continue unless _.isObject(customColumn)
regex = customColumn['pattern']
continue unless regex
return parseInt(customColumn['column']) if new RegExp(regex).test(path)
@defaultColumn
updateGuide: ->
column = @getGuideColumn(@editor.getPath(), @defaultColumn)
if column > 0