Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add logger #64

Merged
merged 2 commits into from
Jul 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions lib/auto_config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { CompositeDisposable, Disposable } from "atom";
import { logger } from "./logger";
import type { StatusBar, Tile } from "atom/status-bar";
//https://github.com/atom/atom-select-list/pull/31
//import {SelectListView} from "atom-select-list";

interface SelectListView {
selectItem(item: Record<string, unknown> | string): Promise<void>;
confirmSelection(): void;
cancelSelection(): void;
}
import type SelectListView from "atom-select-list";

type modes = "deno" | "node";
let statusBarElement: HTMLAnchorElement;
Expand Down Expand Up @@ -79,7 +73,7 @@ function changeMode() {
return;
}
const newMode: modes = atom.config.get("atom-ide-deno.modes.currentMode");
// console.log(`Mode change to ${newMode}`);
logger.log(`Mode change to ${newMode}`);
if (newMode == "deno") {
statusBarElement.innerText = "Deno";
statusBarElement.classList.remove("status-bar-icon-node");
Expand Down
8 changes: 4 additions & 4 deletions lib/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// using custom formatter
// atom-ide-ui(old package) has meny bug at formatting.
import cp from "child_process";
import { logger } from "./logger";

export const options = {
check: "--check",
Expand Down Expand Up @@ -30,13 +30,13 @@ export function formatFile(
filePath: string,
): Promise<{ error: Error | null; stdout: string; stderr: string }> {
const commandOption = ["fmt", ...options, filePath];
// console.log("env: ", process.env);
// console.log(denoPath, ...commandOption);
logger.log("env: ", process.env);
logger.log(denoPath, ...commandOption);
return new Promise((resolve) => {
cp.execFile(denoPath, commandOption, {
env: process.env,
}, (error, stdout, stderr) => {
// console.log({ error, stdout, stderr });
logger.log({ error, stdout, stderr });
resolve({ error, stdout, stderr });
});
});
Expand Down
41 changes: 41 additions & 0 deletions lib/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Logger } from "atom-languageclient";

class CustomLogger implements Logger {
#shouldOutput = false;
/**
* usage:
* ```
* atom.config.observe("packageName.debugMode", logger.observer)
* ```
*/
observer = (newValue: boolean) => {
// 矢印関数なのでthisをbindする必要が無い
this.#shouldOutput = newValue;
};
warn(...args: any[]) {
if (this.#shouldOutput) {
console.warn(...args);
}
}
error(...args: any[]) {
if (this.#shouldOutput) {
console.error(...args);
}
}
info(...args: any[]) {
if (this.#shouldOutput) {
console.info(...args);
}
}
log(...args: any[]) {
if (this.#shouldOutput) {
console.log(...args);
}
}
debug(...args: any[]) {
if (this.#shouldOutput) {
console.debug(...args);
}
}
}
export const logger = new CustomLogger();
46 changes: 15 additions & 31 deletions lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import type { atomConfig } from "./config";
import * as formatter from "./formatter";
import * as autoConfig from "./auto_config";
import { menu } from "../menus/main.json";
import { logger } from "./logger";

import {
AutoLanguageClient,
Convert,
FilteredLogger,
} from "atom-languageclient";
import { AutoLanguageClient, Convert } from "atom-languageclient";
import type {
ActiveServer,
LanguageClientConnection,
LanguageServerProcess,
Logger,
} from "atom-languageclient";
import type { ServerManager } from "atom-languageclient/lib/server-manager";
import type { Point } from "atom";
Expand All @@ -34,17 +32,8 @@ const getDenoPath = (): string =>

class DenoLanguageClient extends AutoLanguageClient {
config: atomConfig = config;
_isDebug!: boolean;
_emptyConnection!: LanguageClientConnection;
subscriptions!: CompositeDisposable;
async setDebugMode(isDebug: boolean) {
this._isDebug = isDebug;
}
debugLog(...msg: any[]) {
if (this._isDebug) {
console.trace(...msg);
}
}
getGrammarScopes() {
return [
"source.js",
Expand All @@ -64,7 +53,7 @@ class DenoLanguageClient extends AutoLanguageClient {
return "deno-language-server";
}
getInitializeParams(...args: [string, LanguageServerProcess]) {
this.debugLog("initializing...");
logger.log("initializing...");
const initializationOptions = atom.config.get("atom-ide-deno.lspFlags");
//filter empty string
initializationOptions.importMap = initializationOptions.importMap || void 0;
Expand All @@ -80,15 +69,15 @@ class DenoLanguageClient extends AutoLanguageClient {
} catch (e) {
console.log(e);
}
this.debugLog(initializationOptions);
logger.log(initializationOptions);
//https://github.com/denoland/deno/pull/8850
//enableフラグが必要
return Object.assign(super.getInitializeParams(...args), {
initializationOptions,
});
}
activate() {
this.debugLog("activating...");
logger.log("activating...");
import("atom-package-deps").then((mod) =>
mod.install("atom-ide-deno", true)
);
Expand All @@ -98,25 +87,22 @@ class DenoLanguageClient extends AutoLanguageClient {
autoConfig.activate({ grammarScopes: this.getGrammarScopes() });
}
async deactivate() {
this.debugLog("deactivating...");
logger.log("deactivating...");
autoConfig.deactivate();
await super.deactivate();
this.subscriptions?.dispose();
}
restartAllServers(...args: []) {
this.debugLog("restart Deno Language server");
logger.log("restart Deno Language server");
atom.notifications.addInfo("restart Deno Language server");
return super.restartAllServers(...args);
}
getLogger() {
return new FilteredLogger(
console,
(lebel) => this._isDebug || ["warn", "error"].includes(lebel),
);
getLogger(): Logger {
return logger;
}
async getDefinition(...args: [TextEditor, Point]) {
const res = await super.getDefinition(...args);
this.debugLog(res);
logger.log(res);
if (res == null) return null;
const { definitions, ...others } = res;
// `deno:/` から始まるカスタムリクエストは相対パスとして解釈されてしまう
Expand All @@ -137,7 +123,7 @@ class DenoLanguageClient extends AutoLanguageClient {
};
}
startServerProcess(_projectPath: string) {
console.log("Starting deno language server");
logger.log("Starting deno language server");
return cp.spawn(getDenoPath(), ["lsp"], { env: process.env });
}
//custom request util
Expand Down Expand Up @@ -228,7 +214,7 @@ class DenoLanguageClient extends AutoLanguageClient {
}
async formatAll() {
for (const projectPath of atom.project.getPaths()) {
// console.log(`format: ${projectPath}`);
logger.log(`format: ${projectPath}`);
const { stderr } = await formatter.formatFile(
getDenoPath(),
[
Expand Down Expand Up @@ -307,7 +293,7 @@ function onActivate(denoLS: DenoLanguageClient) {
{ oldValue = {}, newValue }: { oldValue?: any; newValue: any },
keyPathbase: string[],
) {
// console.log("atom-ide-deno config change caught");
logger.log("atom-ide-deno config change caught");
clearTimeout(inputTimeoutId);
let isDefferRestart = false;
//keyboardInputsKeyPathに含まれるキーの値が変更されていれば、実行を延期(間引く)
Expand Down Expand Up @@ -346,9 +332,7 @@ function onActivate(denoLS: DenoLanguageClient) {
"atom-ide-deno.path",
(v) => restartServer(v, ["path"]),
),
atom.config.observe("atom-ide-deno.advanced.debugMode", (newValue) => {
denoLS.setDebugMode(newValue);
}),
atom.config.observe("atom-ide-deno.advanced.debugMode", logger.observer),
//virtual documentを表示
atom.workspace.addOpener((filePath) => {
if (!filePath.startsWith("deno://")) {
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
"typescript": "~4.3.4"
},
"devDependencies": {
"deno-bin": "^1.10.1",
"@types/atom": "1.40.10",
"@types/atom": "^1.40.11",
"@types/node": "^14.14.37",
"atom-select-list": "^0.8.0",
"atom-select-list": "^0.8.1",
"deno-bin": "^1.10.1",
"vscode-languageserver-protocol": "^3.16.0"
},
"atomTranspilers": [
Expand Down