From 5996fe6cae992cf149b2c86ef86c58a33c209c37 Mon Sep 17 00:00:00 2001 From: Ugur Aslan Date: Fri, 6 Dec 2024 13:24:09 +0100 Subject: [PATCH 1/6] fixed eslint quoting issue --- .eslintrc.json | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 7d45d78..74d71e0 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -38,7 +38,10 @@ ], "quotes": [ "error", - "single" + "single", + { + "avoidEscape": true + } ], "semi": [ "error", @@ -60,9 +63,14 @@ }, "overrides": [ { - "files": ["*.js"], + "files": [ + "*.js" + ], "rules": { - "semi": ["error", "never"] + "semi": [ + "error", + "never" + ] } } ] From 8304251375a04ff7f3e04d2ed0a5abdcfca8b288 Mon Sep 17 00:00:00 2001 From: Ugur Aslan Date: Fri, 6 Dec 2024 13:25:18 +0100 Subject: [PATCH 2/6] some cleanup and formatting --- src/completionItems/elementProps.js | 21 ++++--------------- src/completionProviders/templateAttributes.js | 1 - 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/src/completionItems/elementProps.js b/src/completionItems/elementProps.js index 3003a13..c12747e 100644 --- a/src/completionItems/elementProps.js +++ b/src/completionItems/elementProps.js @@ -27,10 +27,7 @@ let elementPropsParsed = false const parseProps = async () => { // get element/renderer props from Blits codebase const projectRootPath = vscode.workspace.workspaceFolders[0].uri.fsPath - const blitsElementJsPath = path.join( - projectRootPath, - 'node_modules/@lightningjs/blits/src/engines/L3/element.js' - ) + const blitsElementJsPath = path.join(projectRootPath, 'node_modules/@lightningjs/blits/src/engines/L3/element.js') let code = '' try { const fileExists = await fs.pathExists(blitsElementJsPath) @@ -56,17 +53,10 @@ const parseProps = async () => { traverse(ast, { VariableDeclarator({ node }) { // Check if the variable declarator is for 'propsTransformer' - if ( - node.id.name === 'propsTransformer' && - node.init.type === 'ObjectExpression' - ) { + if (node.id.name === 'propsTransformer' && node.init.type === 'ObjectExpression') { // Traverse properties of the 'propsTransformer' object node.init.properties.forEach((property) => { - if ( - property.type === 'ObjectMethod' && - property.kind === 'set' && - property.key.type === 'Identifier' - ) { + if (property.type === 'ObjectMethod' && property.kind === 'set' && property.key.type === 'Identifier') { // Extract the name of the setter method elementProps.push(property.key.name) } @@ -95,10 +85,7 @@ const suggest = async (attributes) => { let completionItems = [] elementProps.forEach((prop) => { if (!attributes.includes(prop)) { - const completionItem = new vscode.CompletionItem( - prop, - vscode.CompletionItemKind.Property - ) + const completionItem = new vscode.CompletionItem(prop, vscode.CompletionItemKind.Property) completionItem.insertText = new vscode.SnippetString(`${prop}="$0"`) completionItems.push(completionItem) } diff --git a/src/completionProviders/templateAttributes.js b/src/completionProviders/templateAttributes.js index 677c079..62ac172 100644 --- a/src/completionProviders/templateAttributes.js +++ b/src/completionProviders/templateAttributes.js @@ -30,7 +30,6 @@ const getCompletionItems = async (document, currentDoc, position, isBlits) => { } if (isCursorInsideTemplate) { - console.log('Cursor inside template') const currentLine = document.lineAt(position).text const { tagName, attributes } = templateHelper.getExistingTagAndAttributes(currentLine) From 53a100bc537a28fd4623a8c39f77922e28de9491 Mon Sep 17 00:00:00 2001 From: Ugur Aslan Date: Fri, 6 Dec 2024 13:26:18 +0100 Subject: [PATCH 3/6] fixes auto formatting bugs --- src/formatters/templateFormatterOnSave.js | 63 +++++++++++++++++++---- src/helpers/template.js | 53 ++++++++++++------- 2 files changed, 89 insertions(+), 27 deletions(-) diff --git a/src/formatters/templateFormatterOnSave.js b/src/formatters/templateFormatterOnSave.js index f24ba9b..62771a8 100644 --- a/src/formatters/templateFormatterOnSave.js +++ b/src/formatters/templateFormatterOnSave.js @@ -45,20 +45,35 @@ function getAutoFormatConfig() { function formatTemplate(template, parser, extraIndentation = '') { const config = getAutoFormatConfig() - template.replace(/^\s+(?=\S)/gm, '') + + if (extraIndentation.length > 0) { + const regex = new RegExp('^' + extraIndentation, 'gm') + template = template.replace(regex, '') + } + + // preserve user-defined trailing whitespace + const trailingWhiteSpace = getTrailingWhiteSpace(template) // process multiline comments template = modifyComments(template) let formattedTemplate = prettier.format(template, { parser, ...config }) if (extraIndentation) { - formattedTemplate = extraIndentation + formattedTemplate.replace(/\n/g, `\n${extraIndentation}`) + formattedTemplate = formattedTemplate.replace(/^/gm, extraIndentation) } + // clean any auto generated trailing white space (respect user's choice here) formattedTemplate = formattedTemplate.trimEnd() + formattedTemplate += trailingWhiteSpace + return formattedTemplate } +function formatScript(script, parser) { + const config = getAutoFormatConfig() + return '\n\n' + prettier.format(script, { parser, ...config }) +} + function modifyComments(str) { const regex = /(\s*)/g @@ -102,20 +117,50 @@ function formatJS(document, currentDoc, fileExtension) { }) } -function formatBlits(document, currentDoc) { +const formatBlits = (document, currentDoc) => { const blitsFileObject = templateHelper.getBlitsFileWithoutLicense(currentDoc) - if (blitsFileObject) { - const { start, end, content } = blitsFileObject + const edits = [] + + if (blitsFileObject.template) { + const { start, end, content } = blitsFileObject.template + try { + const formattedTemplate = formatTemplate(content, 'angular') + edits.push(createEdit(document, start, end, formattedTemplate)) + } catch (err) { + console.log('Blits: error formatting template', err) + } + } + + if (blitsFileObject.script) { + const { start, end, content, lang } = blitsFileObject.script try { - const formattedTemplate = formatTemplate(content, 'vue') - return [createEdit(document, start, end, formattedTemplate)] + const formattedScript = formatScript(content, lang === 'ts' ? 'typescript' : 'babel') //js uses babel parser + edits.push(createEdit(document, start, end, formattedScript)) } catch (err) { - console.log('Blits: error formatting', err) + console.log('Blits: error formatting script', err) } } - return [] + return edits +} + +// keeps the user's trailing whitespace choice +function getTrailingWhiteSpace(text) { + const trailingWhitespaceMatch = text.match(/(\s*)$/) + + if (!trailingWhitespaceMatch) { + return '' + } + + const trailingWhitespace = trailingWhitespaceMatch[0] + const hasNewLine = trailingWhitespace.includes('\n') + + if (!hasNewLine) { + return '' + } else { + return '\n ' + } } module.exports = vscode.workspace.onWillSaveTextDocument((event) => { diff --git a/src/helpers/template.js b/src/helpers/template.js index 40bc665..a879451 100644 --- a/src/helpers/template.js +++ b/src/helpers/template.js @@ -212,26 +212,44 @@ const getTemplateTextForBlits = (document, text, includeTemplateTags = false) => const getBlitsFileWithoutLicense = (text) => { // Regular expression to match a block comment at the start of the file const licenseRegex = /^\s*\/\*[\s\S]*?\*\/\s*/ - const match = text.match(licenseRegex) - if (match) { - const licenseEnd = match[0].length - const contentWithoutLicense = text.slice(licenseEnd).trim() + const licenseEnd = match ? match[0].length : 0 + const contentWithoutLicense = text.slice(licenseEnd).trim() - return { - content: contentWithoutLicense, - start: licenseEnd, - end: text.length, + const result = {} + + // Extract template part + const templateRegex = /