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

Honor Python's coding directive #68

Merged
merged 1 commit into from
Sep 8, 2021
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
4 changes: 2 additions & 2 deletions assets/languages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ Cython:
codemirror_mode: python
codemirror_mime_type: text/x-cython
language_id: 79
comment_style_id: Hashtag
comment_style_id: PythonStyle
D:
type: programming
color: "#ba595e"
Expand Down Expand Up @@ -4441,7 +4441,7 @@ Python:
- python3
- rusthon
language_id: 303
comment_style_id: Hashtag
comment_style_id: PythonStyle
Python console:
type: programming
group: Python
Expand Down
7 changes: 7 additions & 0 deletions assets/styles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,10 @@
start: "'"
middle: "'"
end: "'"

- id: PythonStyle
# (interpreter binary and encoding comment) | (only interpreter binary) | (only encoding comment)
after: '(?m)(^*#!.+$\n^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+).*$)|(^*#!.+$)|(^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+).*$)'
start: '#'
middle: '#'
end: '#'
2 changes: 1 addition & 1 deletion pkg/comments/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestCommentStyle(t *testing.T) {
commentStyleID string
}{
{filename: "Test.java", commentStyleID: "SlashAsterisk"},
{filename: "Test.py", commentStyleID: "Hashtag"},
{filename: "Test.py", commentStyleID: "PythonStyle"},
}
for _, test := range tests {
t.Run(test.filename, func(t *testing.T) {
Expand Down
34 changes: 33 additions & 1 deletion pkg/header/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestRewriteContent(t *testing.T) {
print_string "hello worlds!\n";;
`},
{
name: "Python with Shebang",
name: "Python with interpreter binary",
style: comments.FileCommentStyle("test.py"),
content: `#!/usr/bin/env python3
if __name__ == '__main__':
Expand All @@ -100,6 +100,38 @@ if __name__ == '__main__':
# Apache License 2.0
# http://www.apache.org/licenses/LICENSE-2.0
# Apache License 2.0
if __name__ == '__main__':
print('Hello World')
`},
{
name: "Python with interpreter binary and encoding",
style: comments.FileCommentStyle("test.py"),
content: `#!/usr/bin/env python3
# -*- coding: latin-1 -*-
if __name__ == '__main__':
print('Hello World')
`,
licenseHeader: getLicenseHeader("test.py", t.Error),
expectedContent: `#!/usr/bin/env python3
# -*- coding: latin-1 -*-
# Apache License 2.0
# http://www.apache.org/licenses/LICENSE-2.0
# Apache License 2.0
if __name__ == '__main__':
print('Hello World')
`},
{
name: "Python with encoding",
style: comments.FileCommentStyle("test.py"),
content: `# -*- coding: latin-1 -*-
if __name__ == '__main__':
print('Hello World')
`,
licenseHeader: getLicenseHeader("test.py", t.Error),
expectedContent: `# -*- coding: latin-1 -*-
# Apache License 2.0
# http://www.apache.org/licenses/LICENSE-2.0
# Apache License 2.0
if __name__ == '__main__':
print('Hello World')
`},
Expand Down