Skip to content

Instantly share code, notes, and snippets.

@kattrali
Created September 27, 2011 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kattrali/1245069 to your computer and use it in GitHub Desktop.
Save kattrali/1245069 to your computer and use it in GitHub Desktop.
finding the proper grammar for a given scope in a Redcar tab
module Redcar
class Comment
java_import 'com.redcareditor.mate.Grammar'
# Find a map of comment symbols for a grammar, using the a tab's cursor scope
def self.find_by_scope tab
# doc.cursor_scope is returned as space-delimited string of applicable
# scopes, from most general, to most specific. For example,
# in an erb file, the top scope would be something like 'text.html.ruby',
# and inside a ruby block would be 'source.ruby.embedded.html'.
scopes = tab.edit_view.document.cursor_scope.split(" ").reverse
# search for the most specific scope possible, then default to a more
# general one if none available
scope = scopes.detect {|s| Grammar.find_by_scope_name(s)}
if scope and grammar = Grammar.find_by_scope_name(scope)
name = grammar.name
comment_map[name]
end
end
end
end
# Issue:
# Grammar doesn't seem to match a grammar to some of the embedded scopes.
# For instance, 'source.ruby.embedded.html' resolves to "HTML (Rails)"
# instead of "Ruby".
# Possible solution:
# Truncate scopes to the first two terms when no match is found
# ('source.ruby.embedded.html' -> 'source.ruby') ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment