Skip to content

Scope from, import, and as the same way in imports. #165

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

Merged
merged 1 commit into from
Oct 21, 2018
Merged
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
64 changes: 49 additions & 15 deletions grammars/MagicPython.cson
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,32 @@ repository:
name: "storage.type.function.python"
match: "\\b((async\\s+)?\\s*def)\\b"
}
{
name: "keyword.control.flow.python"
comment: '''
if `as` is eventually followed by `:` or line continuation
it's probably control flow like:
with foo as bar, \\
Foo as Bar:
try:
do_stuff()
except Exception as e:
pass

'''
match: "\\b(?<!\\.)as\\b(?=.*[:\\\\])"
}
{
name: "keyword.control.import.python"
comment: "other legal use of `as` is in an import"
match: "\\b(?<!\\.)as\\b"
}
{
name: "keyword.control.flow.python"
match: '''
(?x)
\\b(?<!\\.)(
as | async | continue | del | assert | break | finally | for
async | continue | del | assert | break | finally | for
| from | elif | else | if | except | pass | raise
| return | try | while | with
)\\b
Expand Down Expand Up @@ -997,27 +1017,41 @@ repository:
match: "(}(?!}))"
import:
comment: '''
Import statements
Import statements used to correctly mark `from`, `import`, and `as`

'''
patterns: [
{
match: '''
(?x)
\\s* \\b(from) \\s*(\\.+)\\s* (import\\b)?

'''
captures:
begin: "\\b(?<!\\.)(from)\\b(?=.+import)"
end: "$|(?=import)"
beginCaptures:
"1":
name: "keyword.control.import.python"
"2":
patterns: [
{
name: "punctuation.separator.period.python"
"3":
name: "keyword.control.import.python"
match: "\\.+"
}
{
include: "#expression"
}
]
}
{
name: "keyword.control.import.python"
match: "\\b(?<!\\.)import\\b"
begin: "\\b(?<!\\.)(import)\\b"
end: "$"
beginCaptures:
"1":
name: "keyword.control.import.python"
patterns: [
{
name: "keyword.control.import.python"
match: "\\b(?<!\\.)as\\b"
}
{
include: "#expression"
}
]
}
]
"class-declaration":
Expand Down Expand Up @@ -1837,13 +1871,13 @@ repository:
(?x)
\\b(?:
(
and | as | assert | async | await | break | class | continue | def
and | assert | async | await | break | class | continue | def
| del | elif | else | except | finally | for | from | global
| if | in | is | (?<=\\.)lambda | lambda(?=\\s*[\\.=])
| nonlocal | not | or | pass | raise | return | try | while | with
| yield
) | (
import
as | import
)
)\\b

Expand Down
82 changes: 65 additions & 17 deletions grammars/MagicPython.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -416,13 +416,37 @@
<key>match</key>
<string>\b((async\s+)?\s*def)\b</string>
</dict>
<dict>
<key>name</key>
<string>keyword.control.flow.python</string>
<key>comment</key>
<string>if `as` is eventually followed by `:` or line continuation
it's probably control flow like:
with foo as bar, \
Foo as Bar:
try:
do_stuff()
except Exception as e:
pass
</string>
<key>match</key>
<string>\b(?&lt;!\.)as\b(?=.*[:\\])</string>
</dict>
<dict>
<key>name</key>
<string>keyword.control.import.python</string>
<key>comment</key>
<string>other legal use of `as` is in an import</string>
<key>match</key>
<string>\b(?&lt;!\.)as\b</string>
</dict>
<dict>
<key>name</key>
<string>keyword.control.flow.python</string>
<key>match</key>
<string>(?x)
\b(?&lt;!\.)(
as | async | continue | del | assert | break | finally | for
async | continue | del | assert | break | finally | for
| from | elif | else | if | except | pass | raise
| return | try | while | with
)\b
Expand Down Expand Up @@ -1531,39 +1555,63 @@
<key>import</key>
<dict>
<key>comment</key>
<string>Import statements
<string>Import statements used to correctly mark `from`, `import`, and `as`
</string>
<key>patterns</key>
<array>
<dict>
<key>match</key>
<string>(?x)
\s* \b(from) \s*(\.+)\s* (import\b)?
</string>
<key>captures</key>
<key>begin</key>
<string>\b(?&lt;!\.)(from)\b(?=.+import)</string>
<key>end</key>
<string>$|(?=import)</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>keyword.control.import.python</string>
</dict>
<key>2</key>
</dict>
<key>patterns</key>
<array>
<dict>
<key>name</key>
<string>punctuation.separator.period.python</string>
<key>match</key>
<string>\.+</string>
</dict>
<key>3</key>
<dict>
<key>include</key>
<string>#expression</string>
</dict>
</array>
</dict>
<dict>
<key>begin</key>
<string>\b(?&lt;!\.)(import)\b</string>
<key>end</key>
<string>$</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>keyword.control.import.python</string>
</dict>
</dict>
</dict>
<dict>
<key>name</key>
<string>keyword.control.import.python</string>
<key>match</key>
<string>\b(?&lt;!\.)import\b</string>
<key>patterns</key>
<array>
<dict>
<key>name</key>
<string>keyword.control.import.python</string>
<key>match</key>
<string>\b(?&lt;!\.)as\b</string>
</dict>
<dict>
<key>include</key>
<string>#expression</string>
</dict>
</array>
</dict>
</array>
</dict>
Expand Down Expand Up @@ -2836,13 +2884,13 @@ indirectly through syntactic constructs
<string>(?x)
\b(?:
(
and | as | assert | async | await | break | class | continue | def
and | assert | async | await | break | class | continue | def
| del | elif | else | except | finally | for | from | global
| if | in | is | (?&lt;=\.)lambda | lambda(?=\s*[\.=])
| nonlocal | not | or | pass | raise | return | try | while | with
| yield
) | (
import
as | import
)
)\b
</string>
Expand Down
46 changes: 34 additions & 12 deletions grammars/src/MagicPython.syntax.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,25 @@ repository:
patterns:
- name: storage.type.function.python
match: \b((async\s+)?\s*def)\b
- name: keyword.control.flow.python
comment: |
if `as` is eventually followed by `:` or line continuation
it's probably control flow like:
with foo as bar, \
Foo as Bar:
try:
do_stuff()
except Exception as e:
pass
match: \b(?<!\.)as\b(?=.*[:\\])
- name: keyword.control.import.python
comment: other legal use of `as` is in an import
match: \b(?<!\.)as\b
- name: keyword.control.flow.python
match: |
(?x)
\b(?<!\.)(
as | async | continue | del | assert | break | finally | for
async | continue | del | assert | break | finally | for
| from | elif | else | if | except | pass | raise
| return | try | while | with
)\b
Expand Down Expand Up @@ -842,17 +856,25 @@ repository:

import:
comment: |
Import statements
Import statements used to correctly mark `from`, `import`, and `as`
patterns:
- match: |
(?x)
\s* \b(from) \s*(\.+)\s* (import\b)?
captures:
- begin: \b(?<!\.)(from)\b(?=.+import)
end: $|(?=import)
beginCaptures:
'1': {name: keyword.control.import.python}
'2': {name: punctuation.separator.period.python}
'3': {name: keyword.control.import.python}
- name: keyword.control.import.python
match: \b(?<!\.)import\b
patterns:
- name: punctuation.separator.period.python
match: \.+
- include: '#expression'

- begin: \b(?<!\.)(import)\b
end: $
beginCaptures:
'1': {name: keyword.control.import.python}
patterns:
- name: keyword.control.import.python
match: \b(?<!\.)as\b
- include: '#expression'

class-declaration:
patterns:
Expand Down Expand Up @@ -1385,13 +1407,13 @@ repository:
(?x)
\b(?:
(
and | as | assert | async | await | break | class | continue | def
and | assert | async | await | break | class | continue | def
| del | elif | else | except | finally | for | from | global
| if | in | is | (?<=\.)lambda | lambda(?=\s*[\.=])
| nonlocal | not | or | pass | raise | return | try | while | with
| yield
) | (
import
as | import
)
)\b
captures:
Expand Down
Loading