From 0aa8a65903e48930ebd2061666073f0e1a59d3e5 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Fri, 28 Dec 2012 08:44:57 -0800 Subject: [PATCH] Support using custom guide columns via config --- .../wrap-guide/spec/wrap-guide-spec.coffee | 42 ++++++------------- src/packages/wrap-guide/src/wrap-guide.coffee | 26 +++++++----- 2 files changed, 29 insertions(+), 39 deletions(-) diff --git a/src/packages/wrap-guide/spec/wrap-guide-spec.coffee b/src/packages/wrap-guide/spec/wrap-guide-spec.coffee index 1bc562624..6e38845be 100644 --- a/src/packages/wrap-guide/spec/wrap-guide-spec.coffee +++ b/src/packages/wrap-guide/spec/wrap-guide-spec.coffee @@ -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() diff --git a/src/packages/wrap-guide/src/wrap-guide.coffee b/src/packages/wrap-guide/src/wrap-guide.coffee index 8442cc736..53f62c289 100644 --- a/src/packages/wrap-guide/src/wrap-guide.coffee +++ b/src/packages/wrap-guide/src/wrap-guide.coffee @@ -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