Merge pull request #34 from github/cschleiden/improve-logging
Add log level and log via connection
This commit is contained in:
@@ -5,13 +5,12 @@ import {
|
||||
HoverParams,
|
||||
InitializeParams,
|
||||
InitializeResult,
|
||||
ProposedFeatures,
|
||||
TextDocumentPositionParams,
|
||||
TextDocuments,
|
||||
TextDocumentSyncKind
|
||||
} from "vscode-languageserver/node";
|
||||
|
||||
import {hover, validate} from "@github/actions-languageservice";
|
||||
import {hover, registerLogger, setLogLevel, validate} from "@github/actions-languageservice";
|
||||
import {TextDocument} from "vscode-languageserver-textdocument";
|
||||
import {contextProviders} from "./context-providers";
|
||||
import {InitializationOptions, RepositoryContext} from "./initializationOptions";
|
||||
@@ -19,21 +18,19 @@ import {onCompletion} from "./on-completion";
|
||||
import {TTLCache} from "./utils/cache";
|
||||
import {valueProviders} from "./value-providers";
|
||||
|
||||
// Create a connection for the server, using Node's IPC as a transport.
|
||||
// Also include all preview / proposed LSP features.
|
||||
const connection = createConnection(ProposedFeatures.all);
|
||||
|
||||
// Create a simple text document manager.
|
||||
const connection = createConnection();
|
||||
const documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
|
||||
|
||||
let sessionToken: string | undefined;
|
||||
let repos: RepositoryContext[] = [];
|
||||
const cache = new TTLCache();
|
||||
|
||||
let hasConfigurationCapability = false;
|
||||
let hasWorkspaceFolderCapability = false;
|
||||
let hasDiagnosticRelatedInformationCapability = false;
|
||||
|
||||
// Register remote console logger with language service
|
||||
registerLogger(connection.console);
|
||||
|
||||
connection.onInitialize((params: InitializeParams) => {
|
||||
const capabilities = params.capabilities;
|
||||
|
||||
@@ -50,6 +47,10 @@ connection.onInitialize((params: InitializeParams) => {
|
||||
repos = options.repos;
|
||||
}
|
||||
|
||||
if (options.logLevel !== undefined) {
|
||||
setLogLevel(options.logLevel);
|
||||
}
|
||||
|
||||
const result: InitializeResult = {
|
||||
capabilities: {
|
||||
textDocumentSync: TextDocumentSyncKind.Full,
|
||||
@@ -72,14 +73,6 @@ connection.onInitialize((params: InitializeParams) => {
|
||||
return result;
|
||||
});
|
||||
|
||||
connection.onInitialized(() => {
|
||||
if (hasWorkspaceFolderCapability) {
|
||||
connection.workspace.onDidChangeWorkspaceFolders(_event => {
|
||||
connection.console.log("Workspace folder change event received.");
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// The content of a text document has changed. This event is emitted
|
||||
// when the text document first opened or when its content has changed.
|
||||
documents.onDidChangeContent(change => {
|
||||
@@ -97,11 +90,6 @@ async function validateTextDocument(textDocument: TextDocument): Promise<void> {
|
||||
connection.sendDiagnostics({uri: textDocument.uri, diagnostics: result});
|
||||
}
|
||||
|
||||
connection.onDidChangeWatchedFiles(_change => {
|
||||
// Monitored files have change in VSCode
|
||||
connection.console.log("We received an file change event");
|
||||
});
|
||||
|
||||
// This handler provides the initial list of the completion items.
|
||||
connection.onCompletion(async ({position, textDocument}: TextDocumentPositionParams): Promise<CompletionItem[]> => {
|
||||
return await onCompletion(
|
||||
@@ -114,8 +102,7 @@ connection.onCompletion(async ({position, textDocument}: TextDocumentPositionPar
|
||||
});
|
||||
|
||||
connection.onHover(async ({position, textDocument}: HoverParams): Promise<Hover | null> => {
|
||||
const r = await hover(documents.get(textDocument.uri)!, position);
|
||||
return r;
|
||||
return hover(documents.get(textDocument.uri)!, position);
|
||||
});
|
||||
|
||||
// Make the text document manager listen on the connection
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
import {LogLevel} from "@github/actions-languageservice/log";
|
||||
export {LogLevel} from "@github/actions-languageservice/log";
|
||||
|
||||
export interface InitializationOptions {
|
||||
sessionToken?: string;
|
||||
|
||||
repos?: RepositoryContext[];
|
||||
|
||||
logLevel?: LogLevel;
|
||||
}
|
||||
|
||||
export interface RepositoryContext {
|
||||
|
||||
@@ -3,6 +3,7 @@ import {TemplateToken} from "@github/actions-workflow-parser/templates/tokens/te
|
||||
import {File} from "@github/actions-workflow-parser/workflows/file";
|
||||
import {Position, TextDocument} from "vscode-languageserver-textdocument";
|
||||
import {Hover} from "vscode-languageserver-types";
|
||||
import {info} from "./log";
|
||||
import {nullTrace} from "./nulltrace";
|
||||
import {findToken} from "./utils/find-token";
|
||||
|
||||
@@ -24,6 +25,8 @@ export async function hover(document: TextDocument, position: Position): Promise
|
||||
|
||||
function getHover(token: TemplateToken): Hover | null {
|
||||
if (token.definition) {
|
||||
info(`Calculating hover for token with definition ${token.definition.key}`);
|
||||
|
||||
let description = "";
|
||||
if (token.description) {
|
||||
description = token.description;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export {complete} from "./complete";
|
||||
export {ContextProviderConfig} from "./context-providers/config";
|
||||
export {hover} from "./hover";
|
||||
export {Logger, LogLevel, registerLogger, setLogLevel} from "./log";
|
||||
export {validate} from "./validate";
|
||||
export {ValueProviderConfig, ValueProviderKind} from "./value-providers/config";
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
export enum LogLevel {
|
||||
Debug = 0,
|
||||
Info = 1,
|
||||
Warn = 2,
|
||||
Error = 3
|
||||
}
|
||||
|
||||
const loggers: Logger[] = [];
|
||||
let logLevel = LogLevel.Warn;
|
||||
|
||||
export interface Logger {
|
||||
/**
|
||||
* Show an error message.
|
||||
*
|
||||
* @param message The message to show.
|
||||
*/
|
||||
error(message: string): void;
|
||||
/**
|
||||
* Show a warning message.
|
||||
*
|
||||
* @param message The message to show.
|
||||
*/
|
||||
warn(message: string): void;
|
||||
/**
|
||||
* Show an information message.
|
||||
*
|
||||
* @param message The message to show.
|
||||
*/
|
||||
info(message: string): void;
|
||||
/**
|
||||
* Log a message.
|
||||
*
|
||||
* @param message The message to log.
|
||||
*/
|
||||
log(message: string): void;
|
||||
}
|
||||
|
||||
export function registerLogger(l: Logger) {
|
||||
loggers.push(l);
|
||||
}
|
||||
|
||||
export function setLogLevel(ll: LogLevel) {
|
||||
logLevel = ll;
|
||||
}
|
||||
|
||||
export function info(message: string): void {
|
||||
if (logLevel > LogLevel.Info) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const l of loggers) {
|
||||
l.info(message);
|
||||
}
|
||||
}
|
||||
|
||||
export function warn(message: string): void {
|
||||
if (logLevel > LogLevel.Warn) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const l of loggers) {
|
||||
l.warn(message);
|
||||
}
|
||||
}
|
||||
|
||||
export function error(message: string): void {
|
||||
if (logLevel > LogLevel.Error) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const l of loggers) {
|
||||
l.error(message);
|
||||
}
|
||||
}
|
||||
|
||||
export function log(message: string): void {
|
||||
for (const l of loggers) {
|
||||
l.log(message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user