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

Add support for extracting styles to tss #1177

Merged
merged 10 commits into from
Oct 29, 2023
38 changes: 29 additions & 9 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,30 +105,37 @@ export function registerProviders(context: vscode.ExtensionContext): void {
contents = document.lineAt(selection.start.line).text;
}

const lineMatches = contents.match(/(\s+)?(?:<(\w+))? ?((?:[.\w]+="[\w./]+" ?)+)(?:(\/>|>(?:.*<\/\w+>)?)?)/);
const lineMatches = contents.match(/(\s+)?(?:<(\w+))?((?:\s*[.\w]+="[\w./%]+")+)\s*(?:(\/>|>(?:.*<\/\w+>)?)?)/);
if (!lineMatches) {
return;
}
const [ , spaces, tag, propertiesString, endingTag ] = lineMatches;

const properties: Record<string, string|Record<string, string>> = {};
const properties: Record<string, string|number|Record<string, string|number>> = {};
const persistProperties: Record<string, string> = {};
for (const property of propertiesString.trim().split(' ')) {
for (const property of propertiesString.replace(/\s+/g, ' ').trim().split(' ')) {
const [ name, value ] = property.split('=');
if (/^(?:on|id|class|platform|ns)/.test(name)) {
persistProperties[name] = value;
continue;
}

if (property.includes('.')) {
let cleanValue;
if (!isNaN(Number(value))) {
cleanValue = Number(value);
} else {
cleanValue = value.replaceAll('"', '');
}

if (name.includes('.')) {
const [ parent, child ] = name.split('.');
if (!properties[parent]) {
properties[parent] = {};
}

(properties[parent] as Record<string, string>)[child] = value;
(properties[parent] as Record<string, string|number>)[child] = cleanValue;
} else {
properties[name] = value;
properties[name] = cleanValue;
}

}
Expand Down Expand Up @@ -163,14 +170,19 @@ export function registerProviders(context: vscode.ExtensionContext): void {
styleName = tag;
}

let styleString = `\n"${styleName}": {`;
let quoteType = '"';
if (tssDocument.getText().includes('\'')) {
quoteType = '\'';
}

let styleString = `\n${quoteType}${styleName}${quoteType}: {`;
for (const [ name, value ] of Object.entries(properties)) {
if (typeof value === 'string') {
styleString = `${styleString}\n\t${name}: ${value}`;
styleString = `${styleString}\n\t${name}: ${wrapValue(value, quoteType)}`;
} else {
let subObject = `\n\t${name}: {`;
for (const [ subName, subValue ] of Object.entries(value)) {
subObject = `${subObject}\n\t\t${subName}: ${subValue}`;
subObject = `${subObject}\n\t\t${subName}: ${wrapValue(subValue, quoteType)}`;
}
subObject = `${subObject}\n\t}`;
styleString = `${styleString}${subObject}`;
Expand Down Expand Up @@ -211,6 +223,14 @@ export function registerProviders(context: vscode.ExtensionContext): void {
});
}

function wrapValue(value: string|number, quote: string) {
if (typeof value !== 'string' || (value.startsWith('Alloy.') || value.startsWith('Ti.') || value.startsWith('Titanium.'))) {
return value;
ewanharris marked this conversation as resolved.
Show resolved Hide resolved
} else {
return `${quote}${value}${quote}`;
}
}

/**
* Generate Alloy and Titanium SDK Completion files
*
Expand Down
Loading