Home

tailwind-ctp-intellisense @984bb5641c120a75d1eeb61fe8cb0b190fcb95ec - refs - log -
-
https://git.jolheiser.com/tailwind-ctp-intellisense.git
Tailwind intellisense + Catppuccin
tailwind-ctp-intellisense / src / extension.ts
- raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* --------------------------------------------------------------------------------------------
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 * ------------------------------------------------------------------------------------------ */
import {
  workspace as Workspace,
  window as Window,
  ExtensionContext,
  TextDocument,
  TextEditor,
  OutputChannel,
  WorkspaceFolder,
  Uri,
  commands,
  Selection,
  Position,
  Range,
  TextEditorRevealType
} from 'vscode'

import {
  LanguageClient,
  LanguageClientOptions,
  TransportKind
} from 'vscode-languageclient'

import { createTreeView } from './treeView'

const CONFIG_GLOB =
  '**/{tailwind,tailwind.config,tailwind-config,.tailwindrc}.js'
let LANGUAGES: string[] = ['html']

let defaultClient: LanguageClient
let clients: Map<string, LanguageClient> = new Map()

let _sortedWorkspaceFolders: string[] | undefined
function sortedWorkspaceFolders(): string[] {
  if (_sortedWorkspaceFolders === void 0) {
    _sortedWorkspaceFolders = Workspace.workspaceFolders
      ? Workspace.workspaceFolders
          .map(folder => {
            let result = folder.uri.toString()
            if (result.charAt(result.length - 1) !== '/') {
              result = result + '/'
            }
            return result
          })
          .sort((a, b) => {
            return a.length - b.length
          })
      : []
  }
  return _sortedWorkspaceFolders
}
Workspace.onDidChangeWorkspaceFolders(
  () => (_sortedWorkspaceFolders = undefined)
)

function getOuterMostWorkspaceFolder(folder: WorkspaceFolder): WorkspaceFolder {
  let sorted = sortedWorkspaceFolders()
  for (let element of sorted) {
    let uri = folder.uri.toString()
    if (uri.charAt(uri.length - 1) !== '/') {
      uri = uri + '/'
    }
    if (uri.startsWith(element)) {
      return Workspace.getWorkspaceFolder(Uri.parse(element))!
    }
  }
  return folder
}

export async function activate(context: ExtensionContext) {
  // let module = context.asAbsolutePath(path.join('server', 'out', 'server.js'))
  let module = '/Users/brad/Code/tailwindcss-language-server/dist/index.js'
  let outputChannel: OutputChannel = Window.createOutputChannel(
    'lsp-multi-server-example'
  )

  async function didOpenTextDocument(document: TextDocument): Promise<void> {
    // if (
    //   document.uri.scheme !== 'file' ||
    //   LANGUAGES.indexOf(document.languageId) === -1
    // ) {
    //   return
    // }

    let uri = document.uri
    let folder = Workspace.getWorkspaceFolder(uri)
    // Files outside a folder can't be handled. This might depend on the language.
    // Single file languages like JSON might handle files outside the workspace folders.
    if (!folder) {
      return
    }

    // If we have nested workspace folders we only start a server on the outer most workspace folder.
    folder = getOuterMostWorkspaceFolder(folder)

    if (!clients.has(folder.uri.toString())) {
      // placeholder
      clients.set(folder.uri.toString(), null)

      let files = await Workspace.findFiles(
        CONFIG_GLOB,
        '**/node_modules/**',
        1
      )
      if (!files.length) return

      let debugOptions = {
        execArgv: ['--nolazy', `--inspect=${6011 + clients.size}`]
      }
      let serverOptions = {
        run: { module, transport: TransportKind.ipc },
        debug: { module, transport: TransportKind.ipc, options: debugOptions }
      }
      let clientOptions: LanguageClientOptions = {
        documentSelector: LANGUAGES.map(language => ({
          scheme: 'file',
          language,
          pattern: `${folder.uri.fsPath}/**/*`
        })),
        diagnosticCollectionName: 'lsp-multi-server-example',
        workspaceFolder: folder,
        outputChannel: outputChannel
      }
      let client = new LanguageClient(
        'lsp-multi-server-example',
        'LSP Multi Server Example',
        serverOptions,
        clientOptions
      )

      client.onReady().then(() => {
        client.onNotification('tailwindcss/foundConfig', configPath => {
          let refresh = createTreeView(configPath)
        })
        client.onNotification(
          'tailwindcss/foundDefinition',
          (configPath, pos) => {
            Workspace.openTextDocument(configPath).then((doc: TextDocument) => {
              Window.showTextDocument(doc).then((editor: TextEditor) => {
                let start = new Position(pos.start.line, pos.start.character)
                let end = new Position(pos.end.line, pos.end.character)
                editor.revealRange(
                  new Range(start, end),
                  TextEditorRevealType.InCenter
                )
                editor.selection = new Selection(start, end)
              })
            })
          }
        )
        commands.registerCommand('tailwindcss.goToDefinition', key => {
          client.sendNotification('tailwindcss/findDefinition', [key])
        })
      })

      // client.onReady().then(() => {
      //   client.onNotification('tailwind/loaded', () => {
      //     console.log('loaded')
      //   })
      // })
      client.start()
      clients.set(folder.uri.toString(), client)
    }
  }

  Workspace.onDidOpenTextDocument(didOpenTextDocument)
  Workspace.textDocuments.forEach(didOpenTextDocument)
  Workspace.onDidChangeWorkspaceFolders(event => {
    for (let folder of event.removed) {
      let client = clients.get(folder.uri.toString())
      if (client) {
        clients.delete(folder.uri.toString())
        client.stop()
      }
    }
  })
}

export function deactivate(): Thenable<void> {
  let promises: Thenable<void>[] = []
  if (defaultClient) {
    promises.push(defaultClient.stop())
  }
  for (let client of clients.values()) {
    if (client) {
      promises.push(client.stop())
    }
  }
  return Promise.all(promises).then(() => undefined)
}