Skip to content

Commit

Permalink
fix: allow to enable/disable the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
sastan committed Mar 22, 2021
1 parent 0bacf61 commit 2901cc3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 41 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,25 @@ Allows to enabling/disabling additional debug information shown in hover and com

Now the debug information is shown.

### Enable

Allows to enabling/disabling the plugin (default: `true`).

```json
{
"compilerOptions": {
"plugins": [
{
"name": "@twind/typescript-plugin",
"debug": false
}
]
}
}
```

Now the plugin is disabled.

## Contribute

Thanks for being willing to contribute!
Expand Down
3 changes: 3 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface TwindPluginConfiguration {
readonly attributes: ReadonlyArray<string>
readonly styles: ReadonlyArray<string>
readonly debug?: boolean
readonly enable: boolean
// Readonly validate: boolean;
// readonly lint: { [key: string]: any };
// readonly emmet: { [key: string]: any };
Expand All @@ -15,6 +16,7 @@ export class ConfigurationManager {
attributes: ['tw', 'class', 'className'],
styles: ['style', 'styled'],
debug: false,
enable: true,
// Validate: true,
// lint: {
// emptyRules: 'ignore',
Expand Down Expand Up @@ -42,6 +44,7 @@ export class ConfigurationManager {
this._configuration = {
...mergedConfig,
debug: 'true' == String(mergedConfig.debug),
enable: 'false' != String(mergedConfig.enable),
}

for (const listener of this._configUpdatedListeners) {
Expand Down
91 changes: 50 additions & 41 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import type * as ts from 'typescript/lib/tsserverlibrary'
import type {
TemplateContext,
TemplateSettings,
} from 'typescript-template-language-service-decorator'
import type { TemplateContext } from 'typescript-template-language-service-decorator'

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

import { ConfigurationManager, TwindPluginConfiguration } from './configuration'
import { TwindLanguageService } from './language-service'
import { StandardTemplateSourceHelper } from './source-helper'
import { LanguageServiceLogger } from './logger'
import { getSourceMatchers } from './source-matcher'

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

Expand Down Expand Up @@ -65,6 +61,11 @@ export class TwindPlugin {
return languageService
}

let enable = this._configManager.config.enable
this._configManager.onUpdatedConfig(() => {
enable = this._configManager.config.enable
})

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

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

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

if (context) {
return ttls.getCompletionEntryDetails(
context,
helper.getRelativePosition(context, position),
name,
)
if (enable) {
const context = helper.getTemplate(fileName, position)

if (context) {
return ttls.getCompletionEntryDetails(
context,
helper.getRelativePosition(context, position),
name,
)
}
}

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

getCompletionsAtPosition: (fileName, position, options) => {
const context = helper.getTemplate(fileName, position)

if (context) {
return translateCompletionInfo(
context,
ttls.getCompletionsAtPosition(context, helper.getRelativePosition(context, position)),
)
if (enable) {
const context = helper.getTemplate(fileName, position)

if (context) {
return translateCompletionInfo(
context,
ttls.getCompletionsAtPosition(context, helper.getRelativePosition(context, position)),
)
}
}

return languageService.getCompletionsAtPosition(fileName, position, options)
},

getQuickInfoAtPosition: (fileName, position) => {
const context = helper.getTemplate(fileName, position)

if (context) {
const quickInfo = ttls.getQuickInfoAtPosition(
context,
helper.getRelativePosition(context, position),
)

if (quickInfo) {
return {
...quickInfo,
textSpan: translateTextSpan(context, quickInfo.textSpan),
if (enable) {
const context = helper.getTemplate(fileName, position)

if (context) {
const quickInfo = ttls.getQuickInfoAtPosition(
context,
helper.getRelativePosition(context, position),
)

if (quickInfo) {
return {
...quickInfo,
textSpan: translateTextSpan(context, quickInfo.textSpan),
}
}
}
}
Expand All @@ -129,14 +136,16 @@ export class TwindPlugin {
getSemanticDiagnostics: (fileName) => {
const diagnostics = [...languageService.getSemanticDiagnostics(fileName)]

helper.getAllTemplates(fileName).forEach((context) => {
for (const diagnostic of ttls.getSemanticDiagnostics(context)) {
diagnostics.push({
...diagnostic,
start: context.node.getStart() + 1 + (diagnostic.start || 0),
})
}
})
if (enable) {
helper.getAllTemplates(fileName).forEach((context) => {
for (const diagnostic of ttls.getSemanticDiagnostics(context)) {
diagnostics.push({
...diagnostic,
start: context.node.getStart() + 1 + (diagnostic.start || 0),
})
}
})
}

return diagnostics
},
Expand All @@ -154,7 +163,7 @@ export class TwindPlugin {

public onConfigurationChanged(config: TwindPluginConfiguration): void {
if (this._logger) {
this._logger.log('onConfigurationChanged')
this._logger.log('onConfigurationChanged: ' + JSON.stringify(config))
}

this._configManager.updateFromPluginConfig(config)
Expand Down

0 comments on commit 2901cc3

Please sign in to comment.