Skip to content

Commit a872f15

Browse files
committed
feat: attributes and styles configurable
1 parent ce3d3d7 commit a872f15

File tree

4 files changed

+47
-35
lines changed

4 files changed

+47
-35
lines changed

src/configuration.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export interface TwindPluginConfiguration {
2-
readonly tags: ReadonlyArray<string>
32
readonly configFile?: string
3+
readonly tags: ReadonlyArray<string>
4+
readonly attributes: ReadonlyArray<string>
5+
readonly styles: ReadonlyArray<string>
46
readonly debug?: boolean
57
// Readonly validate: boolean;
68
// readonly lint: { [key: string]: any };
@@ -10,6 +12,8 @@ export interface TwindPluginConfiguration {
1012
export class ConfigurationManager {
1113
private static readonly defaultConfiguration: TwindPluginConfiguration = {
1214
tags: ['tw', 'apply'],
15+
attributes: ['tw', 'class', 'className'],
16+
styles: ['style', 'styled'],
1317
debug: false,
1418
// Validate: true,
1519
// lint: {
@@ -30,18 +34,15 @@ export class ConfigurationManager {
3034
}
3135

3236
public updateFromPluginConfig(config: Partial<TwindPluginConfiguration> = {}): void {
33-
const { tags, ...mergedConfig } = {
37+
const mergedConfig = {
3438
...ConfigurationManager.defaultConfiguration,
3539
...config,
3640
}
3741

3842
this._configuration = {
3943
...mergedConfig,
4044
debug: 'true' == String(mergedConfig.debug),
41-
tags: this._configuration.tags,
4245
}
43-
;(this._configuration.tags as string[]).length = 0
44-
;(this._configuration.tags as string[]).push(...tags)
4546

4647
for (const listener of this._configUpdatedListeners) {
4748
listener()

src/plugin.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,17 @@ export class TwindPlugin {
6767

6868
const ttls = new TwindLanguageService(this.typescript, info, this._configManager, this._logger)
6969

70-
const templateSettings = getTemplateSettings(this._configManager)
71-
7270
const helper = new StandardTemplateSourceHelper(
7371
this.typescript,
74-
templateSettings,
72+
this._configManager,
7573
new StandardScriptSourceHelper(this.typescript, info.project),
76-
getSourceMatchers(this.typescript, templateSettings),
7774
)
7875

7976
// Set up decorator
8077
return {
8178
...languageService,
8279

80+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8381
getCompletionEntryDetails: (fileName, position, name, ...rest: any[]) => {
8482
const context = helper.getTemplate(fileName, position)
8583

@@ -91,6 +89,7 @@ export class TwindPlugin {
9189
)
9290
}
9391

92+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9493
return (languageService.getCompletionsAtPosition as any)(fileName, position, name, ...rest)
9594
},
9695

@@ -162,18 +161,6 @@ export class TwindPlugin {
162161
}
163162
}
164163

165-
export function getTemplateSettings(configManager: ConfigurationManager): TemplateSettings {
166-
return {
167-
get tags() {
168-
return configManager.config.tags
169-
},
170-
enableForStringWithSubstitutions: true,
171-
getSubstitution(templateString, start, end) {
172-
return `\${${'x'.repeat(end - start - 3)}}`
173-
},
174-
}
175-
}
176-
177164
function isValidTypeScriptVersion(typescript: typeof ts): boolean {
178165
const [major, minor] = typescript.version.split('.')
179166

src/source-helper.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import type {
99
import type ScriptSourceHelper from 'typescript-template-language-service-decorator/lib/script-source-helper'
1010
import type TemplateSourceHelper from 'typescript-template-language-service-decorator/lib/template-source-helper'
1111
import { relative } from 'typescript-template-language-service-decorator/lib/nodes'
12+
import type { ConfigurationManager } from './configuration'
1213
import { match, Matcher } from './match'
14+
import { getSourceMatchers } from './source-matcher'
1315

1416
class PlaceholderSubstituter {
1517
public static replacePlaceholders(
@@ -126,13 +128,35 @@ class StandardTemplateContext /* implements TemplateContext */ {
126128
}
127129
}
128130

131+
export function getTemplateSettings(configManager: ConfigurationManager): TemplateSettings {
132+
return {
133+
get tags() {
134+
return configManager.config.tags
135+
},
136+
enableForStringWithSubstitutions: true,
137+
getSubstitution(templateString, start, end) {
138+
return `\${${'x'.repeat(end - start - 3)}}`
139+
},
140+
}
141+
}
142+
129143
export class StandardTemplateSourceHelper implements TemplateSourceHelper {
144+
private templateSettings: TemplateSettings
145+
private sourceMatchers: Matcher[]
146+
130147
constructor(
131148
private readonly typescript: typeof ts,
132-
private readonly templateStringSettings: TemplateSettings,
149+
private readonly configManager: ConfigurationManager,
133150
private readonly helper: ScriptSourceHelper,
134-
private readonly sourceMatchers: Matcher[],
135-
) {}
151+
) {
152+
this.templateSettings = getTemplateSettings(this.configManager)
153+
this.sourceMatchers = getSourceMatchers(this.typescript, this.configManager.config)
154+
155+
configManager.onUpdatedConfig(() => {
156+
this.templateSettings = getTemplateSettings(this.configManager)
157+
this.sourceMatchers = getSourceMatchers(this.typescript, this.configManager.config)
158+
})
159+
}
136160

137161
public getTemplate(fileName: string, position: number): TemplateContext | undefined {
138162
const node = this.getValidTemplateNode(this.helper.getNode(fileName, position))
@@ -163,7 +187,7 @@ export class StandardTemplateSourceHelper implements TemplateSourceHelper {
163187
fileName,
164188
node,
165189
this.helper,
166-
this.templateStringSettings,
190+
this.templateSettings,
167191
) as TemplateContext
168192
}
169193

@@ -177,7 +201,7 @@ export class StandardTemplateSourceHelper implements TemplateSourceHelper {
177201
fileName,
178202
this.getValidTemplateNode(node) as ts.StringLiteralLike,
179203
this.helper,
180-
this.templateStringSettings,
204+
this.templateSettings,
181205
) as TemplateContext,
182206
)
183207
}
@@ -199,7 +223,7 @@ export class StandardTemplateSourceHelper implements TemplateSourceHelper {
199223
return this.getValidTemplateNode(node.template)
200224
}
201225

202-
// TODO if templateStringSettings.enableForStringWithSubstitutions
226+
// TODO if templateSettings.enableForStringWithSubstitutions
203227
if (this.typescript.isTemplateHead(node) || this.typescript.isTemplateSpan(node)) {
204228
return this.getValidTemplateNode(node.parent)
205229
}

src/source-matcher.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { TemplateSettings } from 'typescript-template-language-service-decorator'
21
import type * as ts from 'typescript/lib/tsserverlibrary'
32
import type { Matcher } from './match'
3+
import type { TwindPluginConfiguration } from './configuration'
44

55
export const getSourceMatchers = (
66
{ SyntaxKind }: typeof ts,
7-
templateStringSettings: TemplateSettings,
7+
config: TwindPluginConfiguration,
88
): Matcher[] => [
99
// tw`...`
1010
{
@@ -13,7 +13,7 @@ export const getSourceMatchers = (
1313
// TODO styled.button, styled()
1414
tag: {
1515
kind: SyntaxKind.Identifier,
16-
text: templateStringSettings.tags,
16+
text: config.tags,
1717
},
1818
},
1919
// tw(...)
@@ -23,15 +23,15 @@ export const getSourceMatchers = (
2323
// TODO styled.button, styled()
2424
expression: {
2525
kind: SyntaxKind.Identifier,
26-
text: templateStringSettings.tags,
26+
text: config.tags,
2727
},
2828
},
2929
// JsxAttribute -> className=""
3030
{
3131
kind: SyntaxKind.JsxAttribute,
3232
name: {
3333
kind: SyntaxKind.Identifier,
34-
text: ['tw', 'class', 'className'],
34+
text: config.attributes,
3535
},
3636
},
3737
// { '@apply': `...` }
@@ -62,7 +62,7 @@ export const getSourceMatchers = (
6262
kind: SyntaxKind.CallExpression,
6363
expression: {
6464
kind: SyntaxKind.Identifier,
65-
text: ['style', 'styled'],
65+
text: config.styles,
6666
},
6767
},
6868
},
@@ -92,7 +92,7 @@ export const getSourceMatchers = (
9292
kind: SyntaxKind.CallExpression,
9393
expression: {
9494
kind: SyntaxKind.Identifier,
95-
text: ['style', 'styled'],
95+
text: config.styles,
9696
},
9797
},
9898
},
@@ -123,7 +123,7 @@ export const getSourceMatchers = (
123123
kind: SyntaxKind.CallExpression,
124124
expression: {
125125
kind: SyntaxKind.Identifier,
126-
text: ['style', 'styled'],
126+
text: config.styles,
127127
},
128128
},
129129
},

0 commit comments

Comments
 (0)