Only by parsing the color scheme xml yourself and matching the scopes manually - for example, the following, when you execute the get_styles_at_cursor
command, will print, to the sublime console, the styles applied at the first cursor position from the view’s current color scheme:
import sublime, sublime_plugin
from lxml import etree
class GetStylesAtCursorCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.parse_color_scheme_file(self.view.settings().get('color_scheme'))
scope_at_position = self.view.scope_name(self.view.sel()[0].begin())
print(self.get_styles_for_scope(scope_at_position))
def parse_color_scheme_file(self, color_scheme_file):
color_scheme_content = sublime.load_resource(color_scheme_file)
color_scheme_xml = etree.fromstring(bytes(color_scheme_content, 'UTF-8'))
self.selectors_in_xml = color_scheme_xml.xpath('//dict/key[.="scope"]/following-sibling::string')
self.default_styles = self.get_styles_from_xml(color_scheme_xml.xpath('/plist/dict/array/dict[1]')[0])
def get_styles_from_xml(self, xml_dict):
styles = dict()
style_keys_xml = xml_dict.xpath('./key[.="settings"]/following-sibling::dict/key')
for key_xml in style_keys_xml:
styles[key_xml.text] = key_xml.xpath('./following-sibling::string/text()')[0]
return styles
def get_styles_for_scope(self, scope):
styles = self.default_styles.copy()
for xml_selector in self.selectors_in_xml:
if sublime.score_selector(scope, xml_selector.text):
selector_styles = self.get_styles_from_xml(xml_selector.getparent())
for style in selector_styles:
styles[style] = selector_styles[style]
return styles
Notes:
- requires the
lxml
dependency to be installed - for example, by installing the XPath plugin
- takes into account the default styles applied by the color scheme for when no scopes match or the scope doesn’t define a particular style
- works on the basis that styles specified later in the xml override styles specified higher up in the document.
Example output:
>>> view.run_command('get_styles_at_cursor')
{'background': '#272822FF', 'foreground': '#F92672', 'caret': '#DCDDD7FF'}