Skip to content

Commit

Permalink
feat(vscode): hide error messages
Browse files Browse the repository at this point in the history
closes #13
  • Loading branch information
mxsdev committed Nov 4, 2022
1 parent 670e069 commit 1103cc0
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 92 deletions.
15 changes: 8 additions & 7 deletions packages/api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@ export type CustomTypeScriptRequestId = CustomTypeScriptRequest["id"]
export type CustomTypeScriptRequestOfId<Id extends CustomTypeScriptRequestId> =
Extract<CustomTypeScriptRequest, { id: Id }>

type TypeTreeResponseBody = {
type CustomTypescriptResponseBodyData = {
id: "type-tree"
typeInfo: TypeInfo | undefined
}

type CustomTypeScriptResponseById = {
"type-tree": TypeTreeResponseBody
}

export type CustomTypeScriptResponse<
Id extends CustomTypeScriptRequestId = CustomTypeScriptRequestId
> = {
body: { __tsExplorerResponse?: CustomTypeScriptResponseBody<Id> }
body: {
__tsExplorerResponse?:
| CustomTypeScriptResponseBody<Id>
| { id: "error"; error: unknown }
}
}

export type CustomTypeScriptResponseBody<
Id extends CustomTypeScriptRequestId = CustomTypeScriptRequestId
> = CustomTypeScriptResponseById[Id]
> = Extract<CustomTypescriptResponseBodyData, { id: Id }>

export type TypescriptContext = {
program: ts.Program
Expand Down
10 changes: 10 additions & 0 deletions packages/typescript-explorer-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@
"title": "Denote Readonly Properties",
"type": "boolean",
"default": false
},
"typescriptExplorer.errorMessages.showDialogue": {
"title": "Show Non-Critical Errors",
"type": "boolean",
"default": false
},
"typescriptExplorer.errorMessages.log": {
"title": "Log Errors",
"type": "boolean",
"default": true
}
}
},
Expand Down
8 changes: 8 additions & 0 deletions packages/typescript-explorer-vscode/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const typeTreeConfigBoolean = {
readonlyEnabled: ["typescriptExplorer.typeTree.readonly.enable"],
} as const

const basicConfig = {
dialogueErrors: "typescriptExplorer.errorMessages.showDialogue",
logErrors: "typescriptExplorer.errorMessages.log",
} as const

const getBoolean = (id: string, defaultValue?: boolean) => () =>
!!config().get(id, defaultValue)
const toggleBoolean = (id: string, defaultValue?: boolean) => () =>
Expand Down Expand Up @@ -54,10 +59,13 @@ export const {
showBaseClassInfo,
showTypeParameterInfo,
readonlyEnabled,
logErrors,
dialogueErrors,
} = {
...mapObject(typeTreeConfigBoolean, ({ value: [id] }) =>
exportBooleanConfig(id)
),
...mapObject(basicConfig, ({ value: id }) => exportBooleanConfig(id)),
}

function config() {
Expand Down
94 changes: 94 additions & 0 deletions packages/typescript-explorer-vscode/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import * as vscode from "vscode"
import type * as Proto from "typescript/lib/protocol"
import {
CustomTypeScriptRequestId,
CustomTypeScriptRequestOfId,
CustomTypeScriptResponse,
CustomTypeScriptResponseBody,
SourceFileLocation,
TypeInfo,
} from "@ts-type-explorer/api"
import {
positionFromLineAndCharacter,
rangeFromLineAndCharacters,
rangeToTextRange,
toFileLocationRequestArgs,
} from "./util"

async function getQuickInfoAtPosition(
fileName: string,
position: vscode.Position
) {
return await vscode.commands
.executeCommand(
"typescript.tsserverRequest",
"quickinfo-full",
toFileLocationRequestArgs(fileName, position)
)
.then((r) => (r as Proto.QuickInfoResponse).body)
}

async function customTypescriptRequest<Id extends CustomTypeScriptRequestId>(
fileName: string,
position: vscode.Position,
request: CustomTypeScriptRequestOfId<Id>
): Promise<CustomTypeScriptResponseBody<Id> | undefined> {
return await vscode.commands
.executeCommand("typescript.tsserverRequest", "completionInfo", {
...toFileLocationRequestArgs(fileName, position),
/**
* We override the "triggerCharacter" property here as a hack so
* that we can send custom commands to TSServer
*/
triggerCharacter: request,
})
.then((val) => {
if (!val) return undefined

const response = val as CustomTypeScriptResponse

if (response.body.__tsExplorerResponse?.id === "error") {
const error = response.body.__tsExplorerResponse.error

const errorObj = new Error(error.message ?? "")
errorObj.stack = error.stack
errorObj.name = error.name ?? errorObj.name

throw errorObj
}

return response.body.__tsExplorerResponse as
| CustomTypeScriptResponseBody<Id>
| undefined
})
}

export function getQuickInfoAtLocation(location: SourceFileLocation) {
return getQuickInfoAtPosition(
location.fileName,
positionFromLineAndCharacter(location.range.start)
)
}

export function getTypeTreeAtLocation(
location: SourceFileLocation
): Promise<TypeInfo | undefined> {
return getTypeTreeAtRange(
location.fileName,
rangeFromLineAndCharacters(location.range.start, location.range.end)
)
}

export function getTypeTreeAtRange(
fileName: string,
range: vscode.Range
): Promise<TypeInfo | undefined> {
return customTypescriptRequest(
fileName,
positionFromLineAndCharacter(range.start),
{
id: "type-tree",
range: rangeToTextRange(range),
}
).then((res) => res?.typeInfo)
}
5 changes: 3 additions & 2 deletions packages/typescript-explorer-vscode/src/state/stateManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { TypeInfo } from "@ts-type-explorer/api"
import * as vscode from "vscode"
import { selectionEnabled } from "../config"
import { getTypeTreeAtRange, isDocumentSupported, showError } from "../util"
import { getTypeTreeAtRange } from "../server"
import { isDocumentSupported, logError, showError } from "../util"
import { TypeTreeItem, TypeTreeProvider } from "../view/typeTreeView"
import { ViewProviders } from "../view/views"

Expand Down Expand Up @@ -171,7 +172,7 @@ export class StateManager {
})
.catch((e) => {
showError("Error getting type information!")
console.error("TypeTreeRequest error", e)
logError("TypeTreeRequest error", e)
})
}
}
86 changes: 13 additions & 73 deletions packages/typescript-explorer-vscode/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import * as vscode from "vscode"
import type * as ts from "typescript"
import {
CustomTypeScriptRequestId,
CustomTypeScriptRequestOfId,
CustomTypeScriptResponse,
SourceFileLocation,
TextRange,
TypeInfo,
} from "@ts-type-explorer/api"
import { TextRange } from "@ts-type-explorer/api"
import type * as Proto from "typescript/lib/protocol"
import { dialogueErrors, logErrors } from "./config"

export const positionToLineAndCharacter = (
position: vscode.Position
Expand All @@ -22,7 +16,7 @@ export const rangeToTextRange = (range: vscode.Range): TextRange => ({
end: positionFromLineAndCharacter(range.end),
})

const toFileLocationRequestArgs = (
export const toFileLocationRequestArgs = (
file: string,
position: vscode.Position
): Proto.FileLocationRequestArgs => ({
Expand All @@ -31,7 +25,7 @@ const toFileLocationRequestArgs = (
offset: position.character + 1,
})

const positionFromLineAndCharacter = ({
export const positionFromLineAndCharacter = ({
line,
character,
}: ts.LineAndCharacter) => new vscode.Position(line, character)
Expand All @@ -51,68 +45,6 @@ export function getTypescriptMd(code: string) {
return mds
}

async function getQuickInfoAtPosition(
fileName: string,
position: vscode.Position
) {
return await vscode.commands
.executeCommand(
"typescript.tsserverRequest",
"quickinfo-full",
toFileLocationRequestArgs(fileName, position)
)
.then((r) => (r as Proto.QuickInfoResponse).body)
}

async function customTypescriptRequest<Id extends CustomTypeScriptRequestId>(
fileName: string,
position: vscode.Position,
request: CustomTypeScriptRequestOfId<Id>
): Promise<CustomTypeScriptResponse<Id> | undefined> {
return await vscode.commands.executeCommand(
"typescript.tsserverRequest",
"completionInfo",
{
...toFileLocationRequestArgs(fileName, position),
/**
* We override the "triggerCharacter" property here as a hack so
* that we can send custom commands to TSServer
*/
triggerCharacter: request,
}
)
}

export function getQuickInfoAtLocation(location: SourceFileLocation) {
return getQuickInfoAtPosition(
location.fileName,
positionFromLineAndCharacter(location.range.start)
)
}

export function getTypeTreeAtLocation(
location: SourceFileLocation
): Promise<TypeInfo | undefined> {
return getTypeTreeAtRange(
location.fileName,
rangeFromLineAndCharacters(location.range.start, location.range.end)
)
}

export function getTypeTreeAtRange(
fileName: string,
range: vscode.Range
): Promise<TypeInfo | undefined> {
return customTypescriptRequest(
fileName,
positionFromLineAndCharacter(range.start),
{
id: "type-tree",
range: rangeToTextRange(range),
}
).then((res) => res?.body.__tsExplorerResponse?.typeInfo)
}

/**
* Smartly set configuration value based on workspace configuration. This will set
* configuration on a workspace level if a workspace value has been set, and will
Expand Down Expand Up @@ -142,7 +74,15 @@ export function smartlySetConfigValue<T>(
}

export function showError(message: string) {
vscode.window.showErrorMessage(message)
if (dialogueErrors.get()) {
vscode.window.showErrorMessage(message)
}
}

export function logError(...messages: unknown[]) {
if (logErrors.get()) {
console.error(...messages)
}
}

export function isDocumentSupported({ languageId }: vscode.TextDocument) {
Expand Down
7 changes: 2 additions & 5 deletions packages/typescript-explorer-vscode/src/view/typeTreeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ import {
} from "../config"
import { markdownDocumentation } from "../markdown"
import { StateManager } from "../state/stateManager"
import {
getQuickInfoAtLocation,
getTypeTreeAtLocation,
rangeFromLineAndCharacters,
} from "../util"
import { rangeFromLineAndCharacters } from "../util"
import { getQuickInfoAtLocation, getTypeTreeAtLocation } from "../server"

const {
None: NoChildren,
Expand Down
23 changes: 18 additions & 5 deletions packages/typescript-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,23 @@ function init(modules: {
entries: [],
}

const responseData = getCustomResponse(ctx, fileName, payload)

if (responseData) {
prior.__tsExplorerResponse = responseData
try {
const responseData = getCustomResponse(ctx, fileName, payload)

if (responseData) {
prior.__tsExplorerResponse = responseData
}
} catch (e: unknown) {
const error = e as Error

prior.__tsExplorerResponse = {
id: "error",
error: {
message: error.message,
stack: error.stack,
name: error.name,
},
}
}

return prior
Expand Down Expand Up @@ -116,7 +129,7 @@ function getCustomResponse(
apiConfig
)

return { typeInfo }
return { id: "type-tree", typeInfo }
}
}
}
Expand Down

0 comments on commit 1103cc0

Please sign in to comment.