diff --git a/spec/editor-view-spec.coffee b/spec/editor-view-spec.coffee index 467266023..7cca1560a 100644 --- a/spec/editor-view-spec.coffee +++ b/spec/editor-view-spec.coffee @@ -3045,3 +3045,12 @@ describe "EditorView", -> editorView.pixelPositionForScreenPosition([0, editor.getTabLength()]) editorView.pixelPositionForScreenPosition([0, editor.getTabLength() + 1]) expect(editorView.measureToColumn.callCount).toBe 0 + + describe "grammar data attributes", -> + it "adds and updates the grammar data attribute based on the current grammar", -> + editorView.attachToDom() + editor.setGrammar(atom.syntax.grammarForScopeName('text.plain')) + expect(editorView.attr('data-grammar')).toBe 'text plain' + + editor.setGrammar(atom.syntax.grammarForScopeName('source.js')) + expect(editorView.attr('data-grammar')).toBe 'source js' diff --git a/src/editor-view.coffee b/src/editor-view.coffee index 94f85392d..812b085f8 100644 --- a/src/editor-view.coffee +++ b/src/editor-view.coffee @@ -560,6 +560,7 @@ class EditorView extends View @trigger 'editor:path-changed' @subscribe @editor, "grammar-changed", => + @addGrammarScopeAttribute() @trigger 'editor:grammar-changed' @subscribe @editor, 'selection-added', (selection) => @@ -581,10 +582,15 @@ class EditorView extends View @trigger 'editor:path-changed' @resetDisplay() + @addGrammarScopeAttribute() if @attached and @editor.buffer.isInConflict() _.defer => @showBufferConflictAlert(@editor) # Display after editor has a chance to display + addGrammarScopeAttribute: -> + grammarScope = @editor.getGrammar()?.scopeName?.replace(/\./g, ' ') + @attr('data-grammar', grammarScope) + getModel: -> @editor diff --git a/src/react-editor-view.coffee b/src/react-editor-view.coffee index 3b166cbbf..643b58f07 100644 --- a/src/react-editor-view.coffee +++ b/src/react-editor-view.coffee @@ -81,8 +81,17 @@ class ReactEditorView extends View @attached = true @component.pollDOM() @focus() if @focusOnAttach + + @addGrammarScopeAttribute() + @subscribe @editor, 'grammar-changed', => + @addGrammarScopeAttribute() + @trigger 'editor:attached', [this] + addGrammarScopeAttribute: -> + grammarScope = @editor.getGrammar()?.scopeName?.replace(/\./g, ' ') + @attr('data-grammar', grammarScope) + scrollTop: (scrollTop) -> if scrollTop? @editor.setScrollTop(scrollTop)