Skip to content
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
85 changes: 38 additions & 47 deletions src/services/ghost/strategies/CommentHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@ import { TextDocument } from "vscode"
export function isCommentLine(line: string, languageId: string): boolean {
const trimmed = line.trim()

// Common single-line comment patterns
const singleLinePatterns = [
const patterns = [
// Structural patterns
/^\/\*$/, // C-style block start
/^<!--$/, // HTML comment start
/^"""$/, // Python docstring
/^'''$/, // Python docstring alternative
// Multi-line patterns
/^\/\*/, // C-style
/^\*/, // Inside C-style block
/^<!--/, // HTML, XML
/^"""/, // Python docstring
/^'''/, // Python docstring alternative
// Single-line patterns
/^\/\//, // JavaScript, TypeScript, C++, Java, etc.
/^#/, // Python, Ruby, Shell, etc.
/^--/, // SQL, Haskell, Lua
Expand All @@ -16,47 +27,15 @@ export function isCommentLine(line: string, languageId: string): boolean {
/^'/, // VB
]

// Multi-line comment patterns
const multiLinePatterns = [
/^\/\*/, // C-style
/^\*/, // Inside C-style block
/^<!--/, // HTML, XML
/^"""/, // Python docstring
/^'''/, // Python docstring alternative
]

// Check single-line patterns
if (singleLinePatterns.some((pattern) => pattern.test(trimmed))) {
// Make sure it contains meaningful text (not just comment syntax)
const withoutCommentSyntax = trimmed.replace(/^(\/\/|#|--|;|%|')\s*/, "")
return withoutCommentSyntax.length > 0
}

// Check multi-line patterns
if (multiLinePatterns.some((pattern) => pattern.test(trimmed))) {
return true
for (const pattern of patterns) {
const match = trimmed.match(pattern)
if (match) {
const withoutSyntax = trimmed.slice(match[0].length).trim()
return withoutSyntax.length > 0
}
}

// Language-specific checks
switch (languageId) {
case "python":
return /^#/.test(trimmed) || /^("""|''')/.test(trimmed)
case "javascript":
case "typescript":
case "javascriptreact":
case "typescriptreact":
return /^(\/\/|\/\*|\*)/.test(trimmed)
case "html":
case "xml":
return /^<!--/.test(trimmed)
case "css":
case "scss":
case "less":
return /^(\/\*|\*)/.test(trimmed)
default:
// Default to common patterns
return /^(\/\/|#|\/\*|\*)/.test(trimmed)
}
return false
}

export function extractComment(document: TextDocument, currentLine: number): string {
Expand Down Expand Up @@ -115,20 +94,32 @@ export function extractComment(document: TextDocument, currentLine: number): str
export function cleanComment(comment: string, languageId: string): string {
const lines = comment.split("\n")
const cleaned = lines.map((line) => {
// Remove common comment prefixes
return line
let cleaned = line.trim()

// Remove common comment prefixes (order matters)
cleaned = cleaned
.replace(/^\/\/\s*/, "") // //
.replace(/^\/\*\s*/, "") // /*
.replace(/^\*\s*/, "") // *
.replace(/\*\/\s*$/, "") // */
.replace(/^\*\s+/, "") // * with required space after
.replace(/^#\s*/, "") // #
.replace(/^--\s*/, "") // --
.replace(/^;\s*/, "") // ;
.replace(/^%\s*/, "") // %
.replace(/^'\s*/, "") // '
.replace(/^<!--\s*/, "") // <!--
.replace(/-->\s*$/, "") // -->
.trim()

// Remove trailing comment syntax and everything after the last occurrence
const lastCommentEnd = cleaned.lastIndexOf("*/")
if (lastCommentEnd !== -1) {
cleaned = cleaned.substring(0, lastCommentEnd).trim()
}

const lastHtmlCommentEnd = cleaned.lastIndexOf("-->")
if (lastHtmlCommentEnd !== -1) {
cleaned = cleaned.substring(0, lastHtmlCommentEnd).trim()
}

return cleaned.trim()
})

return cleaned.filter((line) => line.length > 0).join("\n")
Expand Down
Loading
Loading