Skip to content
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
17 changes: 14 additions & 3 deletions embedding/commentfilter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (
cStyleBlockCommentEnd = "*/"
cStyleDocCommentStart = "/**"
javaTextBlockDelimiter = "\"\"\""
pythonDoubleQuoteBlock = "\"\"\""
pythonSingleQuoteBlock = "'''"
goRawStringDelimiter = "`"
goQuoteChars = "\"'"
jsQuoteChars = "\"'`"
Expand Down Expand Up @@ -62,9 +64,9 @@ var filtersByExtension = map[string]filterEntry{
".proto": filterConfig(MarkerCommentFilter{Syntax: cStyleSyntax}, regularModes),

// Python
".py": filterConfig(MarkerCommentFilter{Syntax: hashLineSyntax}, noneMode),
".pyi": filterConfig(MarkerCommentFilter{Syntax: hashLineSyntax}, noneMode),
".pyw": filterConfig(MarkerCommentFilter{Syntax: hashLineSyntax}, noneMode),
".py": filterConfig(MarkerCommentFilter{Syntax: pythonSyntax}, noneMode),
".pyi": filterConfig(MarkerCommentFilter{Syntax: pythonSyntax}, noneMode),
".pyw": filterConfig(MarkerCommentFilter{Syntax: pythonSyntax}, noneMode),

// YAML
".yml": filterConfig(MarkerCommentFilter{Syntax: hashLineSyntax}, noneMode),
Expand Down Expand Up @@ -131,6 +133,15 @@ var hashLineSyntax = CommentMarker{
QuoteChars: "\"'",
}

var pythonSyntax = CommentMarker{
Inline: []string{"#"},
TextBlocks: []TextBlockMarker{
{Delimiter: pythonDoubleQuoteBlock, Escapes: true},
{Delimiter: pythonSingleQuoteBlock, Escapes: true},
},
QuoteChars: "\"'",
}

var xmlSyntax = CommentMarker{
Block: []BlockMarker{
{Start: "<!--", End: "-->"},
Expand Down
29 changes: 29 additions & 0 deletions embedding/commentfilter/python_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,33 @@ var _ = Describe("Python", func() {

assertFiltered("module.py", RetainNone, lines, expected)
})

It("should strip comments without treating triple-quoted string content as comments", func() {
lines := []string{
"# module comment",
"def message():",
" \"\"\"",
" Keep this # docstring text.",
" \"\"\"",
` escaped = """a\"""" # inline comment`,
" value = '''",
" Keep this # multiline text.",
" ''' # inline comment",
" return value # real comment",
}

expected := []string{
"def message():",
" \"\"\"",
" Keep this # docstring text.",
" \"\"\"",
` escaped = """a\"""" `,
" value = '''",
" Keep this # multiline text.",
" ''' ",
" return value ",
}

assertFiltered("module.py", RetainNone, lines, expected)
})
})
Loading