Skip to content

Commit

Permalink
Add an option to specify the editor grammar for a column
Browse files Browse the repository at this point in the history
  • Loading branch information
abe33 committed Nov 28, 2016
1 parent 5dd9a90 commit ef43eed
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lib/display-column.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class DisplayColumn {
get cellRender () { return this.options.cellRender }
set cellRender (newCellRender) { this.setOption('cellRender', newCellRender) }

get grammarScope () {
return this.options.grammarScope || 'text.plain.null-grammar'
}
set grammarScope (newGrammarScope) {
this.setOption('grammarScope', newGrammarScope)
}

constructor (options = {}) {
this.options = options
this.initID()
Expand Down
4 changes: 4 additions & 0 deletions lib/table-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,8 @@ class TableElement extends HTMLElement {
this.editorElement.style.display = 'block'

const column = this.tableEditor.getScreenColumn(position.column)
this.editor.setGrammar(atom.grammars.grammarForScopeName(column.grammarScope))

this.editorElement.dataset.column = column.name || columnName(position.column)
this.editorElement.dataset.row = position.row + 1

Expand Down Expand Up @@ -1306,6 +1308,8 @@ class TableElement extends HTMLElement {
this.columnUnderEditIndex = columnIndex
const columnRect = headerCell.getBoundingClientRect()

this.editor.setGrammar(atom.grammars.grammarForScopeName('text.plain.null-grammar'))

this.editorElement.style.top = this.toUnit(columnRect.top)
this.editorElement.style.left = this.toUnit(columnRect.left)
this.editorElement.style.minWidth = this.toUnit(columnRect.width)
Expand Down
4 changes: 2 additions & 2 deletions lib/tablr.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ module.exports = {

table.lockModifiedStatus()
table.addColumn('key', {width: 150, align: 'right'})
table.addColumn('value', {width: 150, align: 'center'})
table.addColumn('value', {width: 150, align: 'center', grammarScope: 'source.js'})
table.addColumn('locked', {width: 150, align: 'left'})

const rows = new Array(100).fill().map((v, i) => [
Expand All @@ -196,7 +196,7 @@ module.exports = {

table.lockModifiedStatus()
table.addColumn('key', {width: 150, align: 'right'})
table.addColumn('value', {width: 150, align: 'center'})
table.addColumn('value', {width: 150, align: 'center', grammarScope: 'source.js'})
for (let i = 0; i <= 100; i++) {
table.addColumn(undefined, {width: 150, align: 'left'})
}
Expand Down
4 changes: 3 additions & 1 deletion spec/display-table-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ describe 'DisplayTable', ->
column = displayTable.getScreenColumn(0)
expect(column.width).toEqual(100)
expect(column.align).toEqual('left')
expect(column.grammarScope).toEqual('text.plain.null-grammar')

it 'computes the columns offset', ->
expect(displayTable.getScreenColumnOffsetAt(0)).toEqual(0)
Expand All @@ -150,13 +151,14 @@ describe 'DisplayTable', ->

describe 'with an option object', ->
beforeEach ->
displayTable.addColumn('locked', width: 200, align: 'right')
displayTable.addColumn('locked', width: 200, align: 'right', grammarScope: 'source.js')

it 'creates a new screen column with the given options', ->
expect(displayTable.getScreenColumns().length).toEqual(3)
expect(displayTable.getScreenColumn(2).name).toEqual('locked')
expect(displayTable.getScreenColumn(2).width).toEqual(200)
expect(displayTable.getScreenColumn(2).align).toEqual('right')
expect(displayTable.getScreenColumn(2).grammarScope).toEqual('source.js')

describe 'between two other columns', ->
beforeEach ->
Expand Down
14 changes: 14 additions & 0 deletions spec/table-element-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1711,6 +1711,20 @@ describe 'tableElement', ->
editor = tableElement.querySelector('atom-text-editor').model
expect(editor.getText()).toEqual('x')

describe 'on an table with a column with a grammar', ->
beforeEach ->
waitsForPromise -> atom.packages.activatePackage('language-javascript')

runs ->
tableEditor.getScreenColumn(0).grammarScope = 'source.js'

cell = tableElement.querySelector('tablr-cell[data-column="0"]')
dblclick(cell)

it 'sets the grammar on the editor', ->
editor = tableElement.querySelector('atom-text-editor').model
expect(editor.getGrammar().scopeName).toEqual('source.js')

describe 'double clicking on a cell', ->
beforeEach ->
cell = tableElement.querySelector('tablr-cell:last-child')
Expand Down

0 comments on commit ef43eed

Please sign in to comment.