From c6ca03fa490f412de222a479ec7a91bb361a5cb4 Mon Sep 17 00:00:00 2001 From: Thomas Johansen Date: Wed, 16 Jul 2014 22:36:13 +0200 Subject: [PATCH 1/7] Add grammar scope to EditorView Only works when the `grammar-changed` event has been triggered, and only implemented for Space Pen views. --- src/editor-view.coffee | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/editor-view.coffee b/src/editor-view.coffee index 94f85392d..1352e488a 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", => + @addGrammarScopeClasses() @trigger 'editor:grammar-changed' @subscribe @editor, 'selection-added', (selection) => @@ -585,6 +586,11 @@ class EditorView extends View if @attached and @editor.buffer.isInConflict() _.defer => @showBufferConflictAlert(@editor) # Display after editor has a chance to display + addGrammarScopeClasses: -> + scopeParts = @editor.getGrammar()?.scopeName?.split('.') + classes = scopeParts.join(' ') if scopeParts?.length + @addClass(classes) if classes? + getModel: -> @editor From e50887aab85b1b63c3a4268966781f29b46682ac Mon Sep 17 00:00:00 2001 From: Thomas Johansen Date: Wed, 16 Jul 2014 23:10:21 +0200 Subject: [PATCH 2/7] Use data attribute instead of CSS classes --- src/editor-view.coffee | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/editor-view.coffee b/src/editor-view.coffee index 1352e488a..4cea02d57 100644 --- a/src/editor-view.coffee +++ b/src/editor-view.coffee @@ -560,7 +560,7 @@ class EditorView extends View @trigger 'editor:path-changed' @subscribe @editor, "grammar-changed", => - @addGrammarScopeClasses() + @addGrammarScopeAttribute() @trigger 'editor:grammar-changed' @subscribe @editor, 'selection-added', (selection) => @@ -586,10 +586,9 @@ class EditorView extends View if @attached and @editor.buffer.isInConflict() _.defer => @showBufferConflictAlert(@editor) # Display after editor has a chance to display - addGrammarScopeClasses: -> - scopeParts = @editor.getGrammar()?.scopeName?.split('.') - classes = scopeParts.join(' ') if scopeParts?.length - @addClass(classes) if classes? + addGrammarScopeAttribute: -> + grammarScope = @editor.getGrammar()?.scopeName?.replace(/\./g, ' ') + @attr('data-scope', grammarScope) getModel: -> @editor From 6b1868efd5cd174dd8bcc762755cdcc1c8d1f293 Mon Sep 17 00:00:00 2001 From: Thomas Johansen Date: Wed, 16 Jul 2014 23:12:16 +0200 Subject: [PATCH 3/7] Make sure the scope data attribute is always set --- src/editor-view.coffee | 1 + 1 file changed, 1 insertion(+) diff --git a/src/editor-view.coffee b/src/editor-view.coffee index 4cea02d57..d792a0a88 100644 --- a/src/editor-view.coffee +++ b/src/editor-view.coffee @@ -582,6 +582,7 @@ 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 From 9c154a2f5a580783fbc3dd31057232f5ff54b3c8 Mon Sep 17 00:00:00 2001 From: Thomas Johansen Date: Wed, 16 Jul 2014 23:42:46 +0200 Subject: [PATCH 4/7] Implement support for React Editor --- src/react-editor-view.coffee | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/react-editor-view.coffee b/src/react-editor-view.coffee index 3b166cbbf..11b7900ff 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-scope', grammarScope) + scrollTop: (scrollTop) -> if scrollTop? @editor.setScrollTop(scrollTop) From d3b06542a5c88f48a9fb98e9f291dfb6fa39c149 Mon Sep 17 00:00:00 2001 From: Thomas Johansen Date: Mon, 21 Jul 2014 22:27:55 +0200 Subject: [PATCH 5/7] Change attribute name to `data-grammar` --- src/editor-view.coffee | 2 +- src/react-editor-view.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/editor-view.coffee b/src/editor-view.coffee index d792a0a88..812b085f8 100644 --- a/src/editor-view.coffee +++ b/src/editor-view.coffee @@ -589,7 +589,7 @@ class EditorView extends View addGrammarScopeAttribute: -> grammarScope = @editor.getGrammar()?.scopeName?.replace(/\./g, ' ') - @attr('data-scope', grammarScope) + @attr('data-grammar', grammarScope) getModel: -> @editor diff --git a/src/react-editor-view.coffee b/src/react-editor-view.coffee index 11b7900ff..643b58f07 100644 --- a/src/react-editor-view.coffee +++ b/src/react-editor-view.coffee @@ -90,7 +90,7 @@ class ReactEditorView extends View addGrammarScopeAttribute: -> grammarScope = @editor.getGrammar()?.scopeName?.replace(/\./g, ' ') - @attr('data-scope', grammarScope) + @attr('data-grammar', grammarScope) scrollTop: (scrollTop) -> if scrollTop? From dc55d42491bbc80953862475f8fe1d929c835a1e Mon Sep 17 00:00:00 2001 From: Thomas Johansen Date: Sun, 17 Aug 2014 11:31:24 +0200 Subject: [PATCH 6/7] :white_check_mark: Add test to verify implementation --- spec/editor-view-spec.coffee | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/editor-view-spec.coffee b/spec/editor-view-spec.coffee index 467266023..2a3dfd186 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')).toEqual 'text.plain' + + editor.setGrammar(atom.syntax.grammarForScopeName('source.javascript')) + expect(editorView.attr('data-grammar')).toEqual 'source.javascript' From 4dc89f1b1e81d6d4c7a261ee89932b7592dc5471 Mon Sep 17 00:00:00 2001 From: Thomas Johansen Date: Sun, 24 Aug 2014 14:14:26 +0200 Subject: [PATCH 7/7] :bug: Fix incorrect assertions --- spec/editor-view-spec.coffee | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/editor-view-spec.coffee b/spec/editor-view-spec.coffee index 2a3dfd186..7cca1560a 100644 --- a/spec/editor-view-spec.coffee +++ b/spec/editor-view-spec.coffee @@ -3050,7 +3050,7 @@ describe "EditorView", -> 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')).toEqual 'text.plain' + expect(editorView.attr('data-grammar')).toBe 'text plain' - editor.setGrammar(atom.syntax.grammarForScopeName('source.javascript')) - expect(editorView.attr('data-grammar')).toEqual 'source.javascript' + editor.setGrammar(atom.syntax.grammarForScopeName('source.js')) + expect(editorView.attr('data-grammar')).toBe 'source js'