-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #13
- Loading branch information
Showing
8 changed files
with
156 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters