Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4fe3ae5

Browse files
authored
feat(vscode): use LogOutputChannel to log info (#5125)
1 parent 0bfe7f2 commit 4fe3ae5

File tree

6 files changed

+76
-17
lines changed

6 files changed

+76
-17
lines changed

packages-integrations/language-server/src/core/context.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export class ContextManager {
4949
this.connection.console.log(message)
5050
}
5151

52+
private warn(message: string) {
53+
this.connection.console.warn(message)
54+
}
55+
5256
isTarget(id: string) {
5357
return Array.from(this.contextsMap.keys()).some(cwd => isSubdir(cwd, id))
5458
}
@@ -128,8 +132,8 @@ export class ContextManager {
128132
// Setup Yarn PnP if present
129133
this.setupYarnPnp(dir)
130134

131-
this.log(`\n-----------`)
132-
this.log(`🛠 Resolving config for ${dir}`)
135+
if (!this.discoveredConfigs.has(dir))
136+
this.log(`🛠 Resolving config for ${dir}`)
133137

134138
// @ts-expect-error support global utils
135139
globalThis.defineNuxtConfig = config => config
@@ -170,8 +174,8 @@ export class ContextManager {
170174
sources = (await context.updateRoot(dir)).sources
171175
}
172176
catch (e: any) {
173-
this.log(`⚠️ Error on loading config. Config directory: ${dir}`)
174-
this.log(String(e.stack ?? e))
177+
this.warn(`⚠️ Error on loading config. Config directory: ${dir}`)
178+
this.warn(String(e.stack ?? e))
175179
return this.finishLoading(dir, null)
176180
}
177181

@@ -228,7 +232,10 @@ export class ContextManager {
228232
}
229233

230234
private logConfigInfo(sources: string[], uno: any) {
231-
this.log(`🛠 New configuration loaded from\n${sources.map(s => ` - ${s}`).join('\n')}`)
235+
const sourcesStr = sources.length === 1
236+
? sources[0]
237+
: `\n${sources.map(s => ` - ${s}`).join('\n')}`
238+
this.log(`🛠 New configuration loaded from ${sourcesStr}`)
232239
this.log(`ℹ️ ${uno.config.presets.length} presets, ${uno.config.rulesSize} rules, ${uno.config.shortcuts.length} shortcuts, ${uno.config.variants.length} variants, ${uno.config.transformers?.length || 0} transformers loaded`)
233240

234241
if (!sources.some(i => unoConfigRE.test(i))) {

packages-integrations/vscode/src/client.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
1-
import type { ExtensionContext } from 'vscode'
1+
import type { ExtensionContext, OutputChannel } from 'vscode'
22
import type { LanguageClientOptions, ServerOptions } from 'vscode-languageclient/node'
33
import path from 'node:path'
4+
import process from 'node:process'
45
import { LanguageClient, TransportKind } from 'vscode-languageclient/node'
56
import { getLanguageIds } from './configs'
67
import { log } from './log'
78

89
let client: LanguageClient | undefined
910

11+
function patchOutputChannel(): OutputChannel {
12+
return {
13+
...log.channel,
14+
appendLine(value: string): void {
15+
// vscode-languageclient prepends "[Level - timestamp] " to every line.
16+
// Strip it so LogOutputChannel doesn't double-prefix the message.
17+
const prefixEnd = value.startsWith('[') ? value.indexOf('] ') : -1
18+
const message = prefixEnd >= 0 ? value.slice(prefixEnd + 2) : value
19+
if (value.startsWith('[Error'))
20+
log.error(message)
21+
else if (value.startsWith('[Warn '))
22+
log.warn(message)
23+
else
24+
log.info(message)
25+
},
26+
}
27+
}
28+
1029
export async function createLanguageClient(
1130
context: ExtensionContext,
1231
): Promise<LanguageClient> {
@@ -18,6 +37,13 @@ export async function createLanguageClient(
1837
run: {
1938
module: serverModule,
2039
transport: TransportKind.ipc,
40+
options: {
41+
env: {
42+
...process.env,
43+
// https://github.com/unocss/unocss/pull/5107 introduces ANSI color outputs which not render well in VS Code's output channel. Setting `NO_COLOR` to disable it.
44+
NO_COLOR: '1',
45+
},
46+
},
2147
},
2248
debug: {
2349
module: serverModule,
@@ -36,7 +62,11 @@ export async function createLanguageClient(
3662
synchronize: {
3763
configurationSection: 'unocss',
3864
},
39-
outputChannel: log,
65+
/**
66+
* use `log.channel` instead if we bump vscode-languageclient to v10
67+
* @see https://github.com/microsoft/vscode-languageserver-node/pull/1630
68+
*/
69+
outputChannel: patchOutputChannel(),
4070
}
4171

4272
client = new LanguageClient(

packages-integrations/vscode/src/commands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export function registerCommands(
1919
vscodeCommands.registerCommand(
2020
commandNames.reload,
2121
async () => {
22-
log.appendLine('♻️ Reloading...')
22+
log.info('♻️ Reloading...')
2323
await client.sendRequest('unocss/reloadConfig')
24-
log.appendLine('🔵 Reloaded.')
24+
log.info('🔵 Reloaded.')
2525

2626
if (decorations) {
2727
await decorations.updateDecorations()

packages-integrations/vscode/src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ import { registerDecorations } from './ui/decorations'
99
import { createStatusBar } from './ui/statusBar'
1010

1111
export async function activate(ext: ExtensionContext) {
12-
log.appendLine(`⚪️ UnoCSS for VS Code v${version}`)
12+
log.info(`⚪️ UnoCSS for VS Code v${version}`)
1313

1414
const projectPath = workspace.workspaceFolders?.[0].uri.fsPath
1515
if (!projectPath) {
16-
log.appendLine('➖ No active workspace found, UnoCSS is disabled')
16+
log.info('➖ No active workspace found, UnoCSS is disabled')
1717
return
1818
}
1919

2020
const config = getConfig()
2121
if (config.disable) {
22-
log.appendLine('➖ Disabled by configuration')
22+
log.info('➖ Disabled by configuration')
2323
return
2424
}
2525

2626
try {
2727
const client = await createLanguageClient(ext)
28-
log.appendLine('🔌 Connecting to Language Server...')
28+
log.info('🔌 Connecting to Language Server...')
2929
await client.start()
3030

3131
// Register VSCode-specific features
@@ -34,8 +34,8 @@ export async function activate(ext: ExtensionContext) {
3434
createStatusBar(ext, client)
3535
}
3636
catch (e: any) {
37-
log.appendLine('❌ Failed to start Language Server')
38-
log.appendLine(String(e.stack ?? e))
37+
log.error('❌ Failed to start Language Server')
38+
log.error(e instanceof Error ? e : String(e.stack ?? e))
3939
}
4040
}
4141

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
import type { LogOutputChannel } from 'vscode'
12
import { window } from 'vscode'
23

3-
export const log = window.createOutputChannel('UnoCSS')
4+
class Logger {
5+
readonly channel: LogOutputChannel
6+
7+
constructor() {
8+
this.channel = window.createOutputChannel('UnoCSS', { log: true })
9+
}
10+
11+
info(message: string): void {
12+
this.channel.info(message)
13+
}
14+
15+
warn(message: string): void {
16+
this.channel.warn(message)
17+
}
18+
19+
error(message: string | Error, ...args: unknown[]): void {
20+
this.channel.error(message, ...args)
21+
}
22+
}
23+
24+
export const log = new Logger()

packages-integrations/vscode/src/ui/decorations.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ExtensionContext, TextEditor } from 'vscode'
22
import type { LanguageClient } from 'vscode-languageclient/node'
33
import { DecorationRangeBehavior, Range, window, workspace } from 'vscode'
44
import { getConfig } from '../configs'
5+
import { log } from '../log'
56

67
function throttle<T extends ((...args: any) => any)>(func: T, timeFrame: number): T {
78
let lastTime = 0
@@ -62,7 +63,7 @@ export function registerDecorations(
6263
}
6364
}
6465
catch (err) {
65-
console.error('[Decorations] Error:', err)
66+
log.error('[Decorations] Error fetching matched positions', err instanceof Error ? err : String(err))
6667
reset(editor)
6768
}
6869
}

0 commit comments

Comments
 (0)