Skip to content

Commit 2901cc3

Browse files
committed
fix: allow to enable/disable the plugin
1 parent 0bacf61 commit 2901cc3

File tree

3 files changed

+72
-41
lines changed

3 files changed

+72
-41
lines changed

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,25 @@ Allows to enabling/disabling additional debug information shown in hover and com
291291

292292
Now the debug information is shown.
293293

294+
### Enable
295+
296+
Allows to enabling/disabling the plugin (default: `true`).
297+
298+
```json
299+
{
300+
"compilerOptions": {
301+
"plugins": [
302+
{
303+
"name": "@twind/typescript-plugin",
304+
"debug": false
305+
}
306+
]
307+
}
308+
}
309+
```
310+
311+
Now the plugin is disabled.
312+
294313
## Contribute
295314

296315
Thanks for being willing to contribute!

src/configuration.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface TwindPluginConfiguration {
44
readonly attributes: ReadonlyArray<string>
55
readonly styles: ReadonlyArray<string>
66
readonly debug?: boolean
7+
readonly enable: boolean
78
// Readonly validate: boolean;
89
// readonly lint: { [key: string]: any };
910
// readonly emmet: { [key: string]: any };
@@ -15,6 +16,7 @@ export class ConfigurationManager {
1516
attributes: ['tw', 'class', 'className'],
1617
styles: ['style', 'styled'],
1718
debug: false,
19+
enable: true,
1820
// Validate: true,
1921
// lint: {
2022
// emptyRules: 'ignore',
@@ -42,6 +44,7 @@ export class ConfigurationManager {
4244
this._configuration = {
4345
...mergedConfig,
4446
debug: 'true' == String(mergedConfig.debug),
47+
enable: 'false' != String(mergedConfig.enable),
4548
}
4649

4750
for (const listener of this._configUpdatedListeners) {

src/plugin.ts

+50-41
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import type * as ts from 'typescript/lib/tsserverlibrary'
2-
import type {
3-
TemplateContext,
4-
TemplateSettings,
5-
} from 'typescript-template-language-service-decorator'
2+
import type { TemplateContext } from 'typescript-template-language-service-decorator'
63

74
import StandardScriptSourceHelper from 'typescript-template-language-service-decorator/lib/standard-script-source-helper'
85

96
import { ConfigurationManager, TwindPluginConfiguration } from './configuration'
107
import { TwindLanguageService } from './language-service'
118
import { StandardTemplateSourceHelper } from './source-helper'
129
import { LanguageServiceLogger } from './logger'
13-
import { getSourceMatchers } from './source-matcher'
1410

1511
// https://github.com/microsoft/typescript-template-language-service-decorator/blob/main/src/standard-template-source-helper.ts#L75
1612

@@ -65,6 +61,11 @@ export class TwindPlugin {
6561
return languageService
6662
}
6763

64+
let enable = this._configManager.config.enable
65+
this._configManager.onUpdatedConfig(() => {
66+
enable = this._configManager.config.enable
67+
})
68+
6869
const ttls = new TwindLanguageService(this.typescript, info, this._configManager, this._logger)
6970

7071
const helper = new StandardTemplateSourceHelper(
@@ -79,46 +80,52 @@ export class TwindPlugin {
7980

8081
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8182
getCompletionEntryDetails: (fileName, position, name, ...rest: any[]) => {
82-
const context = helper.getTemplate(fileName, position)
83-
84-
if (context) {
85-
return ttls.getCompletionEntryDetails(
86-
context,
87-
helper.getRelativePosition(context, position),
88-
name,
89-
)
83+
if (enable) {
84+
const context = helper.getTemplate(fileName, position)
85+
86+
if (context) {
87+
return ttls.getCompletionEntryDetails(
88+
context,
89+
helper.getRelativePosition(context, position),
90+
name,
91+
)
92+
}
9093
}
9194

9295
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9396
return (languageService.getCompletionsAtPosition as any)(fileName, position, name, ...rest)
9497
},
9598

9699
getCompletionsAtPosition: (fileName, position, options) => {
97-
const context = helper.getTemplate(fileName, position)
98-
99-
if (context) {
100-
return translateCompletionInfo(
101-
context,
102-
ttls.getCompletionsAtPosition(context, helper.getRelativePosition(context, position)),
103-
)
100+
if (enable) {
101+
const context = helper.getTemplate(fileName, position)
102+
103+
if (context) {
104+
return translateCompletionInfo(
105+
context,
106+
ttls.getCompletionsAtPosition(context, helper.getRelativePosition(context, position)),
107+
)
108+
}
104109
}
105110

106111
return languageService.getCompletionsAtPosition(fileName, position, options)
107112
},
108113

109114
getQuickInfoAtPosition: (fileName, position) => {
110-
const context = helper.getTemplate(fileName, position)
111-
112-
if (context) {
113-
const quickInfo = ttls.getQuickInfoAtPosition(
114-
context,
115-
helper.getRelativePosition(context, position),
116-
)
117-
118-
if (quickInfo) {
119-
return {
120-
...quickInfo,
121-
textSpan: translateTextSpan(context, quickInfo.textSpan),
115+
if (enable) {
116+
const context = helper.getTemplate(fileName, position)
117+
118+
if (context) {
119+
const quickInfo = ttls.getQuickInfoAtPosition(
120+
context,
121+
helper.getRelativePosition(context, position),
122+
)
123+
124+
if (quickInfo) {
125+
return {
126+
...quickInfo,
127+
textSpan: translateTextSpan(context, quickInfo.textSpan),
128+
}
122129
}
123130
}
124131
}
@@ -129,14 +136,16 @@ export class TwindPlugin {
129136
getSemanticDiagnostics: (fileName) => {
130137
const diagnostics = [...languageService.getSemanticDiagnostics(fileName)]
131138

132-
helper.getAllTemplates(fileName).forEach((context) => {
133-
for (const diagnostic of ttls.getSemanticDiagnostics(context)) {
134-
diagnostics.push({
135-
...diagnostic,
136-
start: context.node.getStart() + 1 + (diagnostic.start || 0),
137-
})
138-
}
139-
})
139+
if (enable) {
140+
helper.getAllTemplates(fileName).forEach((context) => {
141+
for (const diagnostic of ttls.getSemanticDiagnostics(context)) {
142+
diagnostics.push({
143+
...diagnostic,
144+
start: context.node.getStart() + 1 + (diagnostic.start || 0),
145+
})
146+
}
147+
})
148+
}
140149

141150
return diagnostics
142151
},
@@ -154,7 +163,7 @@ export class TwindPlugin {
154163

155164
public onConfigurationChanged(config: TwindPluginConfiguration): void {
156165
if (this._logger) {
157-
this._logger.log('onConfigurationChanged')
166+
this._logger.log('onConfigurationChanged: ' + JSON.stringify(config))
158167
}
159168

160169
this._configManager.updateFromPluginConfig(config)

0 commit comments

Comments
 (0)