Home

tailwind-ctp-intellisense @805ddd28794a012922a245521c4495c7d15506c5 - refs - log -
-
https://git.jolheiser.com/tailwind-ctp-intellisense.git
Tailwind intellisense + Catppuccin
tailwind-ctp-intellisense / src / treeView.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import {
  TreeDataProvider,
  TreeItem,
  TreeItemCollapsibleState,
  window as Window,
  Command,
  Event,
  EventEmitter
} from 'vscode'
import { getSvgColorFromValue, createTempFile } from './util'
import dlv from 'dlv'
import * as path from 'path'

const ICONS = {
  colors: 'palette.svg',
  backgroundColors: 'palette.svg',
  borderColors: 'palette.svg',
  textColors: 'palette.svg',
  svgFill: 'palette.svg',
  svgStroke: 'palette.svg',
  screens: 'devices.svg',
  textSizes: 'format_size.svg',
  fonts: 'title.svg',
  fontWeights: 'format_bold.svg',
  zIndex: 'layers.svg',
  borderWidths: 'border_all.svg',
  shadows: 'flip_to_front.svg',
  borderRadius: 'rounded_corner.svg',
  width: 'straighten.svg',
  minWidth: 'straighten.svg',
  maxWidth: 'straighten.svg',
  height: 'straighten.svg',
  minHeight: 'straighten.svg',
  maxHeight: 'straighten.svg',
  opacity: 'opacity.svg',
  leading: 'format_line_spacing.svg',
  backgroundSize: 'photo_size_select_large.svg',
  padding: 'padding.svg',
  margin: 'select_all.svg',
  negativeMargin: 'select_all.svg',
  tracking: 'tracking.svg'
}

function configValueToString(value: any): string {
  if (Array.isArray(value)) {
    return value.join(', ')
  }
  return value.toString()
}

function isObject(val: any): boolean {
  return val != null && typeof val === 'object' && Array.isArray(val) === false
}

class ConfigItem extends TreeItem {
  constructor(
    public label: string,
    public key: string[],
    public collapsibleState: TreeItemCollapsibleState,
    public description?: string,
    public iconPath?: string,
    public command?: Command
  ) {
    super(label, collapsibleState)
    this.key = key
    this.description = description
    this.iconPath = iconPath
  }
}

class TailwindDataProvider implements TreeDataProvider<ConfigItem> {
  private _onDidChangeTreeData: EventEmitter<ConfigItem | null> = new EventEmitter<ConfigItem | null>()
  readonly onDidChangeTreeData: Event<ConfigItem | null> = this
    ._onDidChangeTreeData.event

  private config: any

  constructor(public configPath: string) {
    this.config = require(configPath)
  }
  public refresh(configPath?: string): void {
    if (configPath) this.configPath = configPath
    delete require.cache[this.configPath]
    this.config = require(this.configPath)
    this._onDidChangeTreeData.fire()
  }
  getTreeItem(element: ConfigItem): ConfigItem {
    return element
  }
  async getChildren(element: ConfigItem): Promise<ConfigItem[]> {
    let command = {
      command: 'tailwindcss.goToDefinition',
      title: 'Go To Definition'
    }
    if (element) {
      let item = dlv(this.config, element.key)
      let children = Object.keys(item).map(key => {
        let isObj = isObject(item[key])
        let child = new ConfigItem(
          key,
          element.key.concat(key),
          isObj
            ? TreeItemCollapsibleState.Collapsed
            : TreeItemCollapsibleState.None,
          isObj ? undefined : configValueToString(item[key]),
          undefined,
          isObj
            ? undefined
            : { ...command, arguments: [element.key.concat(key)] }
        )
        let color = getSvgColorFromValue(item[key])

        if (color) {
          return createTempFile(
            `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><rect x="3.45" y="3.45" width="9.1" height="9.1" fill="${color}" /><rect x="2.8" y="2.8" width="10.4" height="10.4" fill="none" stroke="black" stroke-width="1.3" /></svg>`,
            { postfix: '.svg' }
          ).then(iconPath => {
            child.iconPath = iconPath
            return child
          })
        }

        return child
      })
      return Promise.all(children)
    }

    return Object.keys(this.config)
      .filter(key => ['modules', 'plugins', 'options'].indexOf(key) === -1)
      .map(
        key =>
          new ConfigItem(
            key,
            [key],
            isObject(this.config[key])
              ? TreeItemCollapsibleState.Collapsed
              : TreeItemCollapsibleState.None,
            isObject(this.config[key])
              ? undefined
              : configValueToString(this.config[key]),
            ICONS[key]
              ? path.join(
                  __filename,
                  '..',
                  '..',
                  'resources',
                  'icons',
                  ICONS[key]
                )
              : undefined,
            isObject(this.config[key])
              ? undefined
              : { ...command, arguments: [[key]] }
          )
      )
  }
}

function treeDataProvider1(config): TreeDataProvider<TreeItem> {
  return {
    getChildren: async (element): Promise<TreeItem[]> => {
      if (element) {
        let child = dlv(config, element.label, {})
        let items = Object.keys(child).map(key => {
          let color = getSvgColorFromValue(child[key])

          if (color) {
            return createTempFile(
              `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><rect x="3.45" y="3.45" width="9.1" height="9.1" fill="${color}" /><rect x="2.8" y="2.8" width="10.4" height="10.4" fill="none" stroke="black" stroke-width="1.3" /></svg>`,
              { postfix: '.svg' }
            ).then(iconPath => ({
              label: key,
              description: configValueToString(child[key]),
              iconPath
            }))
          }

          return Promise.resolve({
            label: key,
            description: configValueToString(child[key])
          })
        })
        return Promise.all(items)
      }

      return Object.keys(config)
        .filter(key => ['modules', 'plugins', 'options'].indexOf(key) === -1)
        .map(key => ({
          label: key,
          collapsibleState: isObject(config[key])
            ? TreeItemCollapsibleState.Collapsed
            : TreeItemCollapsibleState.None,
          description: isObject(config[key])
            ? undefined
            : configValueToString(config[key]),
          iconPath: ICONS[key]
            ? path.join(
                __filename,
                '..',
                '..',
                'resources',
                'icons',
                ICONS[key]
              )
            : undefined
        }))
    },
    getTreeItem: (element: TreeItem): TreeItem => {
      return element
    }
  }
}

export function createTreeView(configPath) {
  let provider = new TailwindDataProvider(configPath)
  let view = Window.createTreeView('tailwindcssConfigExplorer', {
    treeDataProvider: provider,
    showCollapseAll: true
  })
  view.reveal(undefined)

  return () => provider.refresh()
}