Skip to content

Commit 805ddd2

Browse files
committed
add tree view go to definition
1 parent b1ee475 commit 805ddd2

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

src/extension.ts

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
commands,
1515
Selection,
1616
Position,
17-
Range
17+
Range,
18+
TextEditorRevealType
1819
} from 'vscode'
1920

2021
import {
@@ -76,34 +77,13 @@ export async function activate(context: ExtensionContext) {
7677
'lsp-multi-server-example'
7778
)
7879

79-
let files = await Workspace.findFiles(CONFIG_GLOB, '**/node_modules/**', 1)
80-
81-
if (!files.length) return
82-
83-
let configPath = files[0].fsPath
84-
delete require.cache[configPath]
85-
86-
let refresh = createTreeView(configPath)
87-
commands.registerCommand('tailwindcss.goToDefinition', () => {
88-
// refresh()
89-
// Window.showInformationMessage('Hello World!')
90-
Workspace.openTextDocument(files[0]).then((doc: TextDocument) => {
91-
Window.showTextDocument(doc).then((editor: TextEditor) => {
92-
let start = new Position(0, 0)
93-
let end = new Position(0, 0)
94-
editor.revealRange(new Range(start, end))
95-
editor.selection = new Selection(start, end)
96-
})
97-
})
98-
})
99-
100-
function didOpenTextDocument(document: TextDocument): void {
101-
if (
102-
document.uri.scheme !== 'file' ||
103-
LANGUAGES.indexOf(document.languageId) === -1
104-
) {
105-
return
106-
}
80+
async function didOpenTextDocument(document: TextDocument): Promise<void> {
81+
// if (
82+
// document.uri.scheme !== 'file' ||
83+
// LANGUAGES.indexOf(document.languageId) === -1
84+
// ) {
85+
// return
86+
// }
10787

10888
let uri = document.uri
10989
let folder = Workspace.getWorkspaceFolder(uri)
@@ -112,10 +92,18 @@ export async function activate(context: ExtensionContext) {
11292
if (!folder) {
11393
return
11494
}
95+
11596
// If we have nested workspace folders we only start a server on the outer most workspace folder.
11697
folder = getOuterMostWorkspaceFolder(folder)
11798

11899
if (!clients.has(folder.uri.toString())) {
100+
let files = await Workspace.findFiles(
101+
CONFIG_GLOB,
102+
'**/node_modules/**',
103+
1
104+
)
105+
if (!files.length) return
106+
119107
let debugOptions = {
120108
execArgv: ['--nolazy', `--inspect=${6011 + clients.size}`]
121109
}
@@ -139,6 +127,32 @@ export async function activate(context: ExtensionContext) {
139127
serverOptions,
140128
clientOptions
141129
)
130+
131+
client.onReady().then(() => {
132+
client.onNotification('tailwindcss/foundConfig', configPath => {
133+
let refresh = createTreeView(configPath)
134+
})
135+
client.onNotification(
136+
'tailwindcss/foundDefinition',
137+
(configPath, pos) => {
138+
Workspace.openTextDocument(configPath).then((doc: TextDocument) => {
139+
Window.showTextDocument(doc).then((editor: TextEditor) => {
140+
let start = new Position(pos[0], pos[1])
141+
let end = new Position(pos[2], pos[3])
142+
editor.revealRange(
143+
new Range(start, end),
144+
TextEditorRevealType.InCenter
145+
)
146+
editor.selection = new Selection(start, end)
147+
})
148+
})
149+
}
150+
)
151+
commands.registerCommand('tailwindcss.goToDefinition', key => {
152+
client.sendNotification('tailwindcss/findDefinition', [key])
153+
})
154+
})
155+
142156
// client.onReady().then(() => {
143157
// client.onNotification('tailwind/loaded', () => {
144158
// console.log('loaded')

src/treeView.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ class TailwindDataProvider implements TreeDataProvider<ConfigItem> {
104104
: TreeItemCollapsibleState.None,
105105
isObj ? undefined : configValueToString(item[key]),
106106
undefined,
107-
isObj ? undefined : command
107+
isObj
108+
? undefined
109+
: { ...command, arguments: [element.key.concat(key)] }
108110
)
109111
let color = getSvgColorFromValue(item[key])
110112

@@ -146,7 +148,9 @@ class TailwindDataProvider implements TreeDataProvider<ConfigItem> {
146148
ICONS[key]
147149
)
148150
: undefined,
149-
isObject(this.config[key]) ? undefined : command
151+
isObject(this.config[key])
152+
? undefined
153+
: { ...command, arguments: [[key]] }
150154
)
151155
)
152156
}

0 commit comments

Comments
 (0)