From 4e6845813620814f97fd9a778d5127ed98a6e117 Mon Sep 17 00:00:00 2001 From: iamcco Date: Sat, 5 Dec 2020 23:03:25 +0800 Subject: [PATCH 1/2] feat(msg): add truncate option to avoid hit-enter --- package.json | 5 +++++ src/index.ts | 22 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index b1f75f2..8e25706 100644 --- a/package.json +++ b/package.json @@ -98,6 +98,11 @@ } } } + }, + "json.truncateMessage": { + "type": "boolean", + "default": false, + "description": "Truncate message avoid hit-enter" } } } diff --git a/src/index.ts b/src/index.ts index adff15e..a9246af 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import { DidChangeConfigurationNotification, Position, CompletionContext, Cancel import catalog from './catalog.json' import { hash } from './utils/hash' import { URI } from 'vscode-uri' -import { fetch, commands, ExtensionContext, events, extensions, LanguageClient, ServerOptions, workspace, services, TransportKind, LanguageClientOptions, ProvideCompletionItemsSignature, ResolveCompletionItemSignature, HandleDiagnosticsSignature } from 'coc.nvim' +import { fetch, commands, ExtensionContext, events, extensions, LanguageClient, ServerOptions, workspace, services, TransportKind, LanguageClientOptions, ProvideCompletionItemsSignature, ResolveCompletionItemSignature, HandleDiagnosticsSignature, MsgTypes } from 'coc.nvim' import { joinPath, RequestService } from './requests' import stripBom from 'strip-bom' @@ -76,7 +76,7 @@ export async function activate(context: ExtensionContext): Promise { let doc = workspace.getDocument(bufnr) if (!doc) return let msg = fileSchemaErrors.get(doc.uri) - if (msg) workspace.showMessage(`Schema error: ${msg}`, 'warning') + if (msg) showMessage(`Schema error: ${msg}`, 'warning') }, null, subscriptions) let serverOptions: ServerOptions = { @@ -117,7 +117,7 @@ export async function activate(context: ExtensionContext): Promise { fileSchemaErrors.set(uri.toString(), schemaResolveDiagnostic.message) let doc = workspace.getDocument(uri) if (doc && doc.uri == uri) { - workspace.showMessage(`Schema error: ${schemaResolveDiagnostic.message}`, 'warning') + showMessage(`Schema error: ${schemaResolveDiagnostic.message}`, 'warning') } next(uri, diagnostics as any) }, @@ -412,3 +412,19 @@ function getSchemaAssociations(_context: ExtensionContext): ISchemaAssociation[] }) return associations } + +async function showMessage(msg: string, type?: MsgTypes) { + const isTruncMsg = workspace.getConfiguration('json').get('truncateMessage', false) + if (!isTruncMsg) { + return workspace.showMessage(msg, type) + } + // message prefix `[coc.nvim] ` + const msgPrefixLen = 11 + const maxWidth = (await workspace.nvim.getOption('columns') as number) - 1 /* for less than columns */ + const displayWidth = await workspace.nvim.call('strdisplaywidth', [msg]) + msgPrefixLen + if (displayWidth > maxWidth) { + const halfLen = Math.floor((maxWidth - 3 /*string `...` length*/ - msgPrefixLen) / 2) + msg = `${msg.slice(0, halfLen)}...${msg.slice(-halfLen)}` + } + workspace.showMessage(msg, type) +} From 9dd224f2814fc80ae6d253a9b96ed506f5c34609 Mon Sep 17 00:00:00 2001 From: iamcco Date: Mon, 7 Dec 2020 14:04:15 +0800 Subject: [PATCH 2/2] feat(msg): append to output channel instead --- package.json | 5 ----- src/index.ts | 22 +++------------------- 2 files changed, 3 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 8e25706..b1f75f2 100644 --- a/package.json +++ b/package.json @@ -98,11 +98,6 @@ } } } - }, - "json.truncateMessage": { - "type": "boolean", - "default": false, - "description": "Truncate message avoid hit-enter" } } } diff --git a/src/index.ts b/src/index.ts index a9246af..3d30af8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import { DidChangeConfigurationNotification, Position, CompletionContext, Cancel import catalog from './catalog.json' import { hash } from './utils/hash' import { URI } from 'vscode-uri' -import { fetch, commands, ExtensionContext, events, extensions, LanguageClient, ServerOptions, workspace, services, TransportKind, LanguageClientOptions, ProvideCompletionItemsSignature, ResolveCompletionItemSignature, HandleDiagnosticsSignature, MsgTypes } from 'coc.nvim' +import { fetch, commands, ExtensionContext, events, extensions, LanguageClient, ServerOptions, workspace, services, TransportKind, LanguageClientOptions, ProvideCompletionItemsSignature, ResolveCompletionItemSignature, HandleDiagnosticsSignature } from 'coc.nvim' import { joinPath, RequestService } from './requests' import stripBom from 'strip-bom' @@ -76,7 +76,7 @@ export async function activate(context: ExtensionContext): Promise { let doc = workspace.getDocument(bufnr) if (!doc) return let msg = fileSchemaErrors.get(doc.uri) - if (msg) showMessage(`Schema error: ${msg}`, 'warning') + if (msg) client.outputChannel.appendLine(`Schema error: ${msg}`) }, null, subscriptions) let serverOptions: ServerOptions = { @@ -117,7 +117,7 @@ export async function activate(context: ExtensionContext): Promise { fileSchemaErrors.set(uri.toString(), schemaResolveDiagnostic.message) let doc = workspace.getDocument(uri) if (doc && doc.uri == uri) { - showMessage(`Schema error: ${schemaResolveDiagnostic.message}`, 'warning') + client.outputChannel.appendLine(`Schema error: ${schemaResolveDiagnostic.message}`) } next(uri, diagnostics as any) }, @@ -412,19 +412,3 @@ function getSchemaAssociations(_context: ExtensionContext): ISchemaAssociation[] }) return associations } - -async function showMessage(msg: string, type?: MsgTypes) { - const isTruncMsg = workspace.getConfiguration('json').get('truncateMessage', false) - if (!isTruncMsg) { - return workspace.showMessage(msg, type) - } - // message prefix `[coc.nvim] ` - const msgPrefixLen = 11 - const maxWidth = (await workspace.nvim.getOption('columns') as number) - 1 /* for less than columns */ - const displayWidth = await workspace.nvim.call('strdisplaywidth', [msg]) + msgPrefixLen - if (displayWidth > maxWidth) { - const halfLen = Math.floor((maxWidth - 3 /*string `...` length*/ - msgPrefixLen) / 2) - msg = `${msg.slice(0, halfLen)}...${msg.slice(-halfLen)}` - } - workspace.showMessage(msg, type) -}