Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for syntax highlighting and completions of extends values #407

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Package/PackageDev.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
// settings file for this to take effect.
"settings.show_quick_edit_icon": true,

// List of patterns for syntax not to be displayed in completions.
// Each theme containing one of the list's strings is hidden.
"settings.exclude_syntax_patterns": [
"*.tmLanguage",
"Packages/zzz A File Icon zzz/**"
],

// Whether or not to keep the scope suffix in the suggested test scopes
// i.e. if the base scope is text.html.markdown
// then suggest meta.example.markdown (true) vs meta.example (false).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ contexts:
captures:
1: string.unquoted.plain.out.yaml storage.type.extends.sublime-syntax
2: punctuation.separator.key-value.yaml
push: expect_extends_list

- match: ((?:hidden_)?file_extensions)\s*(:)(?=\s|$)
captures:
Expand Down Expand Up @@ -162,6 +163,52 @@ contexts:

- include: scope:source.yaml

expect_extends_list:
- meta_content_scope: meta.extends.sublime-syntax
- include: comment
- include: yaml-tags-anchors
# array-like context list
- match: \[
scope: punctuation.definition.array.begin.sublime-syntax
set:
- meta_scope: meta.extends.sublime-syntax meta.flow-sequence.yaml
- match: \]
scope: punctuation.definition.array.end.sublime-syntax
pop: true
- match: ','
scope: punctuation.separator.array-element.sublime-syntax
- match: '{{plain_scalar_but_not_block_key}}'
push: extended_syntax_file
- include: comment
- include: yaml-tags-anchors
# multi-line context list
- match: ^([ ]+)(?=-\s*{{plain_scalar_but_not_block_key}})
set:
- meta_scope: meta.extends.sublime-syntax meta.block-sequence.yaml
# pop off at none-empty line with different indention than first item
- match: ^(?!(\s*$|\1-))
pop: true
- match: '{{plain_scalar_but_not_block_key}}'
push: extended_syntax_file
- include: comment
- include: yaml-block-sequence
- match: \S.*$
scope: invalid.illegal.extends.sublime-syntax
# maybe single include
- match: '{{plain_scalar_but_not_block_key}}'
set:
- meta_scope: meta.extends.sublime-syntax meta.path.sublime-syntax string.unquoted.plain.out.yaml
- include: extended_syntax_file
- match: ^(?=\s*$)
pop: 1

extended_syntax_file:
- meta_scope: meta.path.sublime-syntax string.unquoted.plain.out.yaml
- match: '{{_flow_scalar_end_plain_in}}'
pop: true
- match: /
scope: punctuation.separator.path.sublime-syntax

variables_block:
- meta_scope: meta.block.variables.sublime-syntax
- include: comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,26 @@ version: 2
extends: Packages/Default/Text.sublime-syntax
#^^^^^^ string.unquoted.plain.out.yaml storage.type.extends.sublime-syntax
# ^ punctuation.separator.key-value.yaml
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ string.unquoted.plain.out.yaml
# ^ meta.extends - meta.path
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.extends.sublime-syntax meta.path.sublime-syntax string.unquoted.plain.out.yaml
# ^ punctuation.separator.path.sublime-syntax
# ^ punctuation.separator.path.sublime-syntax
# ^ - meta.extends - meta.path
extends: [ Text.sublime-syntax , Source.sublime-syntax ]
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.extends.sublime-syntax meta.flow-sequence.yaml
# ^ - meta.path
# ^^^^^^^^^^^^^^^^^^^ meta.path.sublime-syntax string.unquoted.plain.out.yaml
# ^^^ - meta.path
# ^^^^^^^^^^^^^^^^^^^^^ meta.path.sublime-syntax string.unquoted.plain.out.yaml
# ^^ - meta.path
extends:
- Packages/Default/Text.sublime-syntax
# <- meta.extends.sublime-syntax meta.block-sequence.yaml
#^^^ meta.extends.sublime-syntax meta.block-sequence.yaml
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.extends.sublime-syntax meta.block-sequence.yaml meta.path.sublime-syntax string.unquoted.plain.out.yaml
# ^ punctuation.separator.path.sublime-syntax
# ^ punctuation.separator.path.sublime-syntax
# ^ meta.extends.sublime-syntax meta.block-sequence.yaml - meta.path
file_extensions: [a, b]
#^^^^^^^^^^^^^^ string.unquoted.plain.out.yaml entity.name.tag.yaml
# ^^^^^^ meta.flow-sequence.yaml, meta.sequence.flow.yaml
Expand Down
47 changes: 47 additions & 0 deletions plugins/syntax_dev/completions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
import re
from collections import namedtuple
from fnmatch import fnmatch
from sublime_lib.resource_path import ResourcePath

import sublime
import sublime_plugin
Expand Down Expand Up @@ -266,6 +268,12 @@ def match_selector(selector, offset=0):
):
result = self._complete_branch_point()

elif match_selector(
"meta.extends",
-1,
):
result = self._complete_syntax_file()

# Auto-completion for variables in match patterns using 'variables' keys
elif match_selector("keyword.other.variable"):
result = self._complete_variable()
Expand Down Expand Up @@ -299,6 +307,45 @@ def _complete_context(self, prefix, locations):
kind=TPL_CONTEXT.kind,
)

def _complete_syntax_file(self):
completions = []
kind = (sublime.KIND_ID_VARIABLE, 's', 'Syntax')

settings = sublime.load_settings("PackageDev.sublime-settings")
excludes = settings.get("settings.exclude_syntax_patterns", [])
if not isinstance(excludes, list):
excludes = []

try:
my_folder = str(ResourcePath.from_file_path(self.view.file_name()).parent)
except (TypeError, ValueError):
my_folder = ""

for syntax in sublime.list_syntaxes():
if any(fnmatch(syntax.path, pattern) for pattern in excludes):
continue
# add relative resource path completion (file name of siblings)
if my_folder:
folder, file = syntax.path.rsplit("/", 1)
if folder == my_folder:
completions.append(
sublime.CompletionItem(
trigger=file,
kind=kind,
annotation="hidden" if syntax.hidden else ""
)
)
# add full resource path
completions.append(
sublime.CompletionItem(
trigger=syntax.path,
kind=kind,
annotation="hidden" if syntax.hidden else ""
)
)

return completions

def _complete_keyword(self, prefix, locations):

def match_selector(selector, offset=0):
Expand Down