{"id":20581026,"url":"https://github.com/coder2mo/codestyleenhancer","last_synced_at":"2025-10-10T10:10:55.649Z","repository":{"id":189428715,"uuid":"680663315","full_name":"Coder2Mo/CodeStyleEnhancer","owner":"Coder2Mo","description":"Automatically adds closing brackets and formatting enhancements to your code in Visual Studio Code","archived":false,"fork":false,"pushed_at":"2023-08-20T04:12:56.000Z","size":50,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-06T12:16:01.588Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Coder2Mo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-20T01:39:41.000Z","updated_at":"2023-08-20T02:04:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"af547064-8cb9-4a3b-88e9-414914180d5c","html_url":"https://github.com/Coder2Mo/CodeStyleEnhancer","commit_stats":null,"previous_names":["coder2mo/codestyleenhancer"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Coder2Mo/CodeStyleEnhancer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Coder2Mo%2FCodeStyleEnhancer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Coder2Mo%2FCodeStyleEnhancer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Coder2Mo%2FCodeStyleEnhancer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Coder2Mo%2FCodeStyleEnhancer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Coder2Mo","download_url":"https://codeload.github.com/Coder2Mo/CodeStyleEnhancer/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Coder2Mo%2FCodeStyleEnhancer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279003467,"owners_count":26083594,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T06:26:44.820Z","updated_at":"2025-10-10T10:10:55.629Z","avatar_url":"https://github.com/Coder2Mo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CodeStyleEnhancer\n\n\u003cdiv align=\"center\"\u003e\n    \u003cimg width=\"500px\" height=\"350px\" alt=\"CodeStyleEnhancer\" src=\"https://github.com/Coder2Mo/CodeStyleEnhancer/blob/main/images/icon.png\"\u003e\n\u003c/div\u003e\n\n\u003cbr\u003e\n\u003cbr\u003e\n\nThis Extension serves as the implementation of a Visual Studio Code extension that provides enhanced auto-closing bracket functionality.\nThe main purpose of the extension is to automatically insert closing brackets, such as parentheses, square brackets, and curly braces, as well as various types of quotes and code blocks in specific scenarios.\nThe extension also offers features like matching bracket highlighting, auto-closing inside comments, and configurable behaviors for different languages.\u003c/p\u003e\u003cp\u003e\nHere's what the code does:\u003c/p\u003e\u003col\u003e\u003cli\u003e\u003cp\u003e\u003cstrong\u003eAuto-Closing Brackets and Quotes:\u003c/strong\u003e The extension automatically inserts closing brackets when an opening bracket is typed.\nIt also handles auto-closing quotes like double quotes, single quotes, and backticks.\u003c/p\u003e\u003c/li\u003e\n\n``` typescript\nif (autoCloseQuotes \u0026\u0026 ['\"', \"'\", '`'].includes(currentLine.charAt(position.character - 1))) {\n    const quoteChar = currentLine.charAt(position.character - 1);\n    const isQuoteEscaped = currentLine.charAt(position.character - 2) === '\\\\';\n\n    if (!isInsideString || (quoteChar === currentLine.charAt(position.character) \u0026\u0026 !isQuoteEscaped)) {\n        const insertPos = new vscode.Position(position.line, position.character);\n        editBuilder.insert(insertPos, quoteChar);\n    }\n}\n// - CoderMo\n```\n\n\u003cli\u003e\u003cp\u003e\u003cstrong\u003eMatching Bracket Highlighting:\u003c/strong\u003e The extension highlights the matching opening bracket when the cursor is near a closing bracket.\u003c/p\u003e\u003c/li\u003e\n\n``` typescript\nconst openBrackets: { [line: number]: number[] } = {};\n\nvscode.window.onDidChangeTextEditorSelection(event =\u003e {\n    if (event.textEditor) {\n        const editor = event.textEditor;\n        const selections = editor.selections;\n        const decorations: vscode.DecorationOptions[] = [];\n\n        selections.forEach(selection =\u003e {\n            const position = selection.active;\n            const line = position.line;\n            const character = position.character;\n            const currentLine = editor.document.lineAt(line).text;\n\n            if (['(', '[', '{'].includes(currentLine.charAt(character))) {\n                if (!openBrackets[line]) {\n                    openBrackets[line] = [];\n                }\n                openBrackets[line].push(character);\n            }\n\n            if ([')', ']', '}'].includes(currentLine.charAt(character))) {\n                const matchingIndex = openBrackets[line]?.pop();\n                if (matchingIndex !== undefined) {\n                    const startPos = new vscode.Position(line, matchingIndex);\n                    const endPos = new vscode.Position(line, character);\n                    decorations.push({\n                        range: new vscode.Range(startPos, endPos),\n                    });\n                }\n            }\n        });\n\n        editor.setDecorations(bracketPairDecorationType, decorations);\n    }\n});\n\n// - CoderMo\n```\n\n\u003cli\u003e\u003cp\u003e\u003cstrong\u003eAuto-Closing Inside Comments:\u003c/strong\u003e The extension optionally closes brackets and quotes inside comments, depending on user configuration.\u003c/p\u003e\u003c/li\u003e\n\n``` typescript\nconst autoCloseInsideComments = config.get('autoCloseInsideComments', false);\n\nconst position = editor.selection.active;\nconst currentLine = editor.document.lineAt(position.line).text;\nconst isInsideComment = currentLine.trim().startsWith('//');\n\nif ((!/\\}$/.test(currentLine) || autoCloseInsideComments) \u0026\u0026\n    !isInsideString \u0026\u0026\n    (!isInsideComment || autoCloseInsideComments)\n) {\n    // ... Rest of the code ...\n}\n\n// - CoderMo\n```\n\n\u003cli\u003e\u003cp\u003e\u003cstrong\u003eAuto-Closing in Markdown Code Blocks:\u003c/strong\u003e The extension can automatically insert closing markers for code blocks in Markdown files.\u003c/p\u003e\u003c/li\u003e\n\n``` typescript\nconst autoCloseInMarkdownCodeBlocks = config.get('autoCloseBracketsEnhanced.autoCloseInMarkdownCodeBlocks', true);\n\nif (!/\\}$/.test(currentLine) \u0026\u0026 !isInsideString \u0026\u0026 !isInsideComment) {\n    const languageId = editor.document.languageId;\n\n    function isInsideMarkdownCodeBlock(document: vscode.TextDocument, position: vscode.Position): boolean {\n        const lineText = document.lineAt(position.line).text;\n        return lineText.trim() === '```';\n    }\n\n    function determineClosingBracketForMarkdown(document: vscode.TextDocument, position: vscode.Position): string | undefined {\n        return '```';\n    }\n\n    if (autoCloseInMarkdownCodeBlocks \u0026\u0026 isInsideMarkdownCodeBlock(editor.document, position)) {\n        const closingBracketText = determineClosingBracketForMarkdown(editor.document, position);\n        if (closingBracketText) {\n            editor.edit(editBuilder =\u003e {\n                editBuilder.insert(position, closingBracketText);\n            });\n        }\n    }\n}\n\n// - CoderMo\n```\n\n\u003cli\u003e\u003cp\u003e\u003cstrong\u003eCustomizable Auto-Close Rules:\u003c/strong\u003e You can define auto-close behavior based on language rules. For example, you can configure whether a closing bracket is placed on the same line or a new line after an opening bracket.\u003c/p\u003e\u003c/li\u003e\n\n``` typescript\nconst languageRules: Record\u003cstring, string\u003e = {\n    'javascript': 'sameLine',\n    'typescript': 'newLineAfter',\n    // Add more languages and rules as needed\n};\n\nconst languageId = editor.document.languageId;\nconst languageSpecificRule = vscode.workspace.getConfiguration('autoCloseBracketsEnhanced').get(`languageRules.${languageId}`, languageRules[languageId]);\nconst autoCloseFormatting = languageSpecificRule || 'sameLine';\n\n// - CoderMo\n```\n\n\u003cli\u003e\u003cp\u003e\u003cstrong\u003eCustom Triggers:\u003c/strong\u003e You can define custom triggers that, when typed, insert specific code snippets.\u003c/p\u003e\u003c/li\u003e\n\n``` typescript\nconst customTriggers = config.get('customTriggers', ['{']);\n\nconst customTriggersMapping: Record\u003cstring, string\u003e = {\n    '[': ']',\n    '(': ')',\n    '{': '}',\n    '\"': '\"',\n    \"'\": \"'\",\n    '\u003c': '\u003e',\n    '`': '`',\n    // Add more trigger mappings as needed\n};\n\nif (customTriggers.includes(currentLine.charAt(position.character - 1))) {\n    const openingTrigger = currentLine.charAt(position.character - 1);\n    const snippet = userSnippets[openingTrigger];\n    if (snippet) {\n        editBuilder.insert(insertPos, snippet);\n    }\n}\n\n// - CoderMo\n```\n\n\u003cli\u003e\u003cp\u003e\u003cstrong\u003eLinting Integration:\u003c/strong\u003e The extension integrates with linting tools (configurable through the \u003ccode\u003elintingTool\u003c/code\u003e setting) and can process linting results. However, the specific linting functionality (e.g., handling linting results) appears to be a work in progress and may need further development to integrate seamlessly.\u003c/p\u003e\u003c/li\u003e\n\n``` typescript\nimport * as vscode from 'vscode';\nimport { spawn } from 'child_process';\nimport { LintingResult, parseLintingResults } from './lintingResult';\n\n// - CoderMo\n```\n\u003cbr\u003e\n\n``` typescript\nasync function runLinting(document: vscode.TextDocument): Promise\u003cLintingResult[]\u003e {\n    // ...\n    const lintingTool = vscode.workspace.getConfiguration().get('autoCloseBracketsEnhanced.lintingTool', 'eslint');\n    const lintingProcess = spawn(lintingTool, [document.uri.fsPath, '--format=json'], { shell: true });\n    \n    // ...\n    lintingProcess.stdout.on('data', (data: Buffer) =\u003e {\n        lintingOutput += data.toString();\n    });\n\n    // ...\n    lintingProcess.on('close', async code =\u003e {\n        if (code === 0) {\n            const rawLintingResults: any[] = JSON.parse(lintingOutput);\n            const lintingResults = await parseLintingResults(rawLintingResults);\n            resolve(lintingResults);\n        } else {\n            reject(new Error('Linting failed'));\n        }\n    });\n}\n\n// - CoderMo\n```\n\u003cbr\u003e\n\n``` typescript\nasync function findMatchingClosingBracket(\n    editor: vscode.TextEditor,\n    position: vscode.Position,\n    currentChar: string,\n    openingChar: string,\n    closingChar: string\n): Promise\u003cvscode.Position | null\u003e {\n    // ...\n    if (lintingEnabled) {\n        try {\n            const lintingResults = await runLinting(editor.document);\n\n            for (const lintingResult of lintingResults) {\n                console.log('Linting Result:', lintingResult);\n                // You can process the linting results here and use the information as needed\n            }\n\n        } catch (error) {\n            console.error('Linting failed:', (error as Error).message);\n        }\n    }\n    // ...\n}\n\n// - CoderMo\n```\n\n\u003cli\u003e\u003cp\u003e\u003cstrong\u003eBracket Pair Decoration:\u003c/strong\u003e The extension provides bracket pair decoration to visually highlight matched opening and closing brackets.\u003c/p\u003e\u003c/li\u003e\n\n``` typescript\nconst openBrackets: { [line: number]: number[] } = {};\n\nvscode.window.onDidChangeTextEditorSelection(event =\u003e {\n    if (event.textEditor) {\n        const editor = event.textEditor;\n        const selections = editor.selections;\n        const decorations: vscode.DecorationOptions[] = [];\n\n        selections.forEach(selection =\u003e {\n            const position = selection.active;\n            const line = position.line;\n            const character = position.character;\n            const currentLine = editor.document.lineAt(line).text;\n\n            if (['(', '[', '{'].includes(currentLine.charAt(character))) {\n                if (!openBrackets[line]) {\n                    openBrackets[line] = [];\n                }\n                openBrackets[line].push(character);\n            }\n\n            if ([')', ']', '}'].includes(currentLine.charAt(character))) {\n                const matchingIndex = openBrackets[line]?.pop();\n                if (matchingIndex !== undefined) {\n                    const startPos = new vscode.Position(line, matchingIndex);\n                    const endPos = new vscode.Position(line, character);\n                    decorations.push({\n                        range: new vscode.Range(startPos, endPos),\n                    });\n                }\n            }\n        });\n\n        editor.setDecorations(bracketPairDecorationType, decorations);\n    }\n});\n\n// - CoderMo\n```\n\n  \u003cli\u003e\u003cp\u003e\u003cstrong\u003e\u003ccode\u003ebraceWrap\u003c/code\u003e Command:\u003c/strong\u003e You can use the \u003ccode\u003ebraceWrap\u003c/code\u003e command to wrap selected code with curly braces.\u003c/p\u003e\u003c/li\u003e\n\n  ``` typescript\nbraceWrapDisposable = vscode.commands.registerCommand('extension.braceWrap', async () =\u003e {\n    const editor = vscode.window.activeTextEditor;\n\n    if (editor) {\n        const selections = editor.selections;\n        const closingBracket = '}';\n\n        editor.edit(editBuilder =\u003e {\n            selections.forEach(selection =\u003e {\n                const startPos = selection.start;\n                const endPos = selection.end;\n                const indentation = editor.document.lineAt(startPos.line).text.match(/^\\s*/)![0];\n\n                editBuilder.insert(startPos, '{');\n                editBuilder.insert(endPos, `${indentation}${closingBracket}`);\n            });\n        });\n    }\n});\n\n// - CoderMo\n```\n\n  \u003cli\u003e\u003cp\u003e\u003cstrong\u003e\u003ccode\u003egoToMatchingBracket\u003c/code\u003e Command:\u003c/strong\u003e The extension provides the \u003ccode\u003egoToMatchingBracket\u003c/code\u003e command to navigate between matching opening and closing brackets.\u003c/p\u003e\u003c/li\u003e\n\n   ``` typescript\nlet goToMatchingBracketDisposable = vscode.commands.registerCommand('extension.goToMatchingBracket', async () =\u003e {\n    const editor = vscode.window.activeTextEditor;\n\n    if (editor) {\n        const position = editor.selection.active;\n        const currentChar = editor.document.getText(new vscode.Range(position, position.translate(0, 1)));\n\n        if (openingBrackets.includes(currentChar) || closingBrackets.includes(currentChar)) {\n            const matchedPosition = await findMatchingBracket(editor, position);\n\n            if (matchedPosition) {\n                editor.selection = new vscode.Selection(matchedPosition, matchedPosition);\n                editor.revealRange(new vscode.Range(matchedPosition, matchedPosition));\n            }\n        }\n    }\n});\n\n// - CoderMo\n```\n  \n  \u003cli\u003e\u003cp\u003e\u003cstrong\u003eFormatting Provider:\u003c/strong\u003e The extension registers a document formatting provider for TypeScript files.\u003c/p\u003e\u003c/li\u003e\u003c/ol\u003e\n\n   ``` typescript\ncontext.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider('typescript', {\n    provideDocumentFormattingEdits(document: vscode.TextDocument): vscode.TextEdit[] {\n        const edits: vscode.TextEdit[] = [];\n        // ... Fill in the edits array with formatting edits ...\n        return edits;\n    }\n}));\n\n// - CoderMo\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoder2mo%2Fcodestyleenhancer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoder2mo%2Fcodestyleenhancer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoder2mo%2Fcodestyleenhancer/lists"}