vscode utils continued
This commit is contained in:
parent
84fda7740c
commit
431345643e
7 changed files with 78 additions and 117 deletions
2
core/index.d.ts
vendored
2
core/index.d.ts
vendored
|
|
@ -687,8 +687,6 @@ export interface IDE {
|
|||
|
||||
// Callbacks
|
||||
onDidChangeActiveTextEditor(callback: (filepath: string) => void): void;
|
||||
|
||||
pathSep(): Promise<string>;
|
||||
}
|
||||
|
||||
// Slash Commands
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ class VsCodeIde implements IDE {
|
|||
|
||||
async gotoDefinition(location: Location): Promise<RangeInFile[]> {
|
||||
const result = await executeGotoProvider({
|
||||
uri: location.filepath,
|
||||
uri: vscode.Uri.parse(location.filepath),
|
||||
line: location.position.line,
|
||||
character: location.position.character,
|
||||
name: "vscode.executeDefinitionProvider",
|
||||
|
|
@ -63,10 +63,10 @@ class VsCodeIde implements IDE {
|
|||
return result;
|
||||
}
|
||||
|
||||
onDidChangeActiveTextEditor(callback: (filepath: string) => void): void {
|
||||
onDidChangeActiveTextEditor(callback: (uri: string) => void): void {
|
||||
vscode.window.onDidChangeActiveTextEditor((editor) => {
|
||||
if (editor) {
|
||||
callback(editor.document.uri.fsPath);
|
||||
callback(editor.document.uri.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -220,7 +220,7 @@ class VsCodeIde implements IDE {
|
|||
};
|
||||
|
||||
async getRepoName(dir: string): Promise<string | undefined> {
|
||||
const repo = await this.getRepo(vscode.Uri.file(dir));
|
||||
const repo = await this.getRepo(dir);
|
||||
const remotes = repo?.state.remotes;
|
||||
if (!remotes) {
|
||||
return undefined;
|
||||
|
|
@ -267,7 +267,7 @@ class VsCodeIde implements IDE {
|
|||
|
||||
readRangeInFile(filepath: string, range: Range): Promise<string> {
|
||||
return this.ideUtils.readRangeInFile(
|
||||
filepath,
|
||||
vscode.Uri.parse(filepath),
|
||||
new vscode.Range(
|
||||
new vscode.Position(range.start.line, range.start.character),
|
||||
new vscode.Position(range.end.line, range.end.character),
|
||||
|
|
@ -279,7 +279,7 @@ class VsCodeIde implements IDE {
|
|||
const pathToLastModified: { [path: string]: number } = {};
|
||||
await Promise.all(
|
||||
files.map(async (file) => {
|
||||
const stat = await vscode.workspace.fs.stat(uriFromFilePath(file));
|
||||
const stat = await vscode.workspace.fs.stat(vscode.Uri.parse(file));
|
||||
pathToLastModified[file] = stat.mtime;
|
||||
}),
|
||||
);
|
||||
|
|
@ -287,8 +287,8 @@ class VsCodeIde implements IDE {
|
|||
return pathToLastModified;
|
||||
}
|
||||
|
||||
async getRepo(dir: vscode.Uri): Promise<Repository | undefined> {
|
||||
return this.ideUtils.getRepo(dir);
|
||||
async getRepo(dir: string): Promise<Repository | undefined> {
|
||||
return this.ideUtils.getRepo(vscode.Uri.parse(dir));
|
||||
}
|
||||
|
||||
async isTelemetryEnabled(): Promise<boolean> {
|
||||
|
|
@ -371,7 +371,7 @@ class VsCodeIde implements IDE {
|
|||
}
|
||||
|
||||
async getWorkspaceDirs(): Promise<string[]> {
|
||||
return this.ideUtils.getWorkspaceDirectories();
|
||||
return this.ideUtils.getWorkspaceDirectories().map((uri) => uri.toString());
|
||||
}
|
||||
|
||||
async getContinueDir(): Promise<string> {
|
||||
|
|
@ -380,7 +380,7 @@ class VsCodeIde implements IDE {
|
|||
|
||||
async writeFile(path: string, contents: string): Promise<void> {
|
||||
await vscode.workspace.fs.writeFile(
|
||||
vscode.Uri.file(path),
|
||||
vscode.Uri.parse(path),
|
||||
Buffer.from(contents),
|
||||
);
|
||||
}
|
||||
|
|
@ -390,7 +390,7 @@ class VsCodeIde implements IDE {
|
|||
}
|
||||
|
||||
async openFile(path: string): Promise<void> {
|
||||
await this.ideUtils.openFile(path);
|
||||
await this.ideUtils.openFile(vscode.Uri.parse(path));
|
||||
}
|
||||
|
||||
async showLines(
|
||||
|
|
@ -402,13 +402,15 @@ class VsCodeIde implements IDE {
|
|||
new vscode.Position(startLine, 0),
|
||||
new vscode.Position(endLine, 0),
|
||||
);
|
||||
openEditorAndRevealRange(filepath, range).then((editor) => {
|
||||
// Select the lines
|
||||
editor.selection = new vscode.Selection(
|
||||
new vscode.Position(startLine, 0),
|
||||
new vscode.Position(endLine, 0),
|
||||
);
|
||||
});
|
||||
openEditorAndRevealRange(vscode.Uri.parse(filepath), range).then(
|
||||
(editor) => {
|
||||
// Select the lines
|
||||
editor.selection = new vscode.Selection(
|
||||
new vscode.Position(startLine, 0),
|
||||
new vscode.Position(endLine, 0),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async runCommand(command: string): Promise<void> {
|
||||
|
|
@ -425,15 +427,14 @@ class VsCodeIde implements IDE {
|
|||
}
|
||||
|
||||
async saveFile(filepath: string): Promise<void> {
|
||||
await this.ideUtils.saveFile(filepath);
|
||||
await this.ideUtils.saveFile(vscode.Uri.parse(filepath));
|
||||
}
|
||||
|
||||
private static MAX_BYTES = 100000;
|
||||
|
||||
async readFile(fileUri: string): Promise<string> {
|
||||
try {
|
||||
filepath = this.ideUtils.getAbsolutePath(filepath);
|
||||
const uri = uriFromFilePath(filepath);
|
||||
const uri = vscode.Uri.parse(fileUri);
|
||||
|
||||
// First, check whether it's a notebook document
|
||||
// Need to iterate over the cells to get full contents
|
||||
|
|
@ -452,16 +453,14 @@ class VsCodeIde implements IDE {
|
|||
}
|
||||
|
||||
// Check whether it's an open document
|
||||
const openTextDocument = vscode.workspace.textDocuments.find(
|
||||
(doc) => doc.uri.fsPath === uri.fsPath,
|
||||
const openTextDocument = vscode.workspace.textDocuments.find((doc) =>
|
||||
URI.equal(doc.uri.toString(), uri.toString()),
|
||||
);
|
||||
if (openTextDocument !== undefined) {
|
||||
return openTextDocument.getText();
|
||||
}
|
||||
|
||||
const fileStats = await vscode.workspace.fs.stat(
|
||||
uriFromFilePath(filepath),
|
||||
);
|
||||
const fileStats = await vscode.workspace.fs.stat(uri);
|
||||
if (fileStats.size > 10 * VsCodeIde.MAX_BYTES) {
|
||||
return "";
|
||||
}
|
||||
|
|
@ -491,7 +490,7 @@ class VsCodeIde implements IDE {
|
|||
}
|
||||
|
||||
async getOpenFiles(): Promise<string[]> {
|
||||
return await this.ideUtils.getOpenFiles();
|
||||
return this.ideUtils.getOpenFiles().map((uri) => uri.toString());
|
||||
}
|
||||
|
||||
async getCurrentFile() {
|
||||
|
|
@ -599,11 +598,12 @@ class VsCodeIde implements IDE {
|
|||
}
|
||||
|
||||
async getBranch(dir: string): Promise<string> {
|
||||
return this.ideUtils.getBranch(vscode.Uri.file(dir));
|
||||
return this.ideUtils.getBranch(vscode.Uri.parse(dir));
|
||||
}
|
||||
|
||||
getGitRootPath(dir: string): Promise<string | undefined> {
|
||||
return this.ideUtils.getGitRoot(dir);
|
||||
async getGitRootPath(dir: string): Promise<string | undefined> {
|
||||
const root = await this.ideUtils.getGitRoot(vscode.Uri.parse(dir));
|
||||
return root?.toString();
|
||||
}
|
||||
|
||||
async listDir(dir: string): Promise<[string, FileType][]> {
|
||||
|
|
|
|||
|
|
@ -9,14 +9,12 @@ import * as vscode from "vscode";
|
|||
|
||||
import type { IDE, Range, RangeInFile, RangeInFileWithContents } from "core";
|
||||
import type Parser from "web-tree-sitter";
|
||||
import {
|
||||
AutocompleteSnippetDeprecated,
|
||||
GetLspDefinitionsFunction,
|
||||
} from "core/autocomplete/types";
|
||||
import { GetLspDefinitionsFunction } from "core/autocomplete/types";
|
||||
import {
|
||||
AutocompleteCodeSnippet,
|
||||
AutocompleteSnippetType,
|
||||
} from "core/autocomplete/snippets/types";
|
||||
import URI from "uri-js";
|
||||
|
||||
type GotoProviderName =
|
||||
| "vscode.executeDefinitionProvider"
|
||||
|
|
@ -26,13 +24,13 @@ type GotoProviderName =
|
|||
| "vscode.executeReferenceProvider";
|
||||
|
||||
interface GotoInput {
|
||||
uri: string;
|
||||
uri: vscode.Uri;
|
||||
line: number;
|
||||
character: number;
|
||||
name: GotoProviderName;
|
||||
}
|
||||
function gotoInputKey(input: GotoInput) {
|
||||
return `${input.name}${input.uri.toString}${input.line}${input.character}`;
|
||||
return `${input.name}${input.uri.toString()}${input.line}${input.character}`;
|
||||
}
|
||||
|
||||
const MAX_CACHE_SIZE = 50;
|
||||
|
|
@ -50,14 +48,14 @@ export async function executeGotoProvider(
|
|||
try {
|
||||
const definitions = (await vscode.commands.executeCommand(
|
||||
input.name,
|
||||
vscode.Uri.parse(input.uri),
|
||||
input.uri,
|
||||
new vscode.Position(input.line, input.character),
|
||||
)) as any;
|
||||
|
||||
const results = definitions
|
||||
.filter((d: any) => (d.targetUri || d.uri) && (d.targetRange || d.range))
|
||||
.map((d: any) => ({
|
||||
filepath: (d.targetUri || d.uri).fsPath,
|
||||
filepath: (d.targetUri || d.uri).toString(),
|
||||
range: d.targetRange || d.range,
|
||||
}));
|
||||
|
||||
|
|
@ -156,7 +154,7 @@ async function crawlTypes(
|
|||
const definitions = await Promise.all(
|
||||
identifierNodes.map(async (node) => {
|
||||
const [typeDef] = await executeGotoProvider({
|
||||
uri: rif.filepath,
|
||||
uri: vscode.Uri.parse(rif.filepath),
|
||||
// TODO: tree-sitter is zero-indexed, but there seems to be an off-by-one
|
||||
// error at least with the .ts parser sometimes
|
||||
line:
|
||||
|
|
@ -184,7 +182,7 @@ async function crawlTypes(
|
|||
!definition ||
|
||||
results.some(
|
||||
(result) =>
|
||||
result.filepath === definition.filepath &&
|
||||
URI.equal(result.filepath, definition.filepath) &&
|
||||
intersection(result.range, definition.range) !== null,
|
||||
)
|
||||
) {
|
||||
|
|
@ -204,7 +202,7 @@ async function crawlTypes(
|
|||
}
|
||||
|
||||
export async function getDefinitionsForNode(
|
||||
uri: string,
|
||||
uri: vscode.Uri,
|
||||
node: Parser.SyntaxNode,
|
||||
ide: IDE,
|
||||
lang: AutocompleteLanguageInfo,
|
||||
|
|
|
|||
|
|
@ -2,8 +2,6 @@ import * as path from "node:path";
|
|||
|
||||
import * as vscode from "vscode";
|
||||
|
||||
import { uriFromFilePath } from "./util/vscode";
|
||||
|
||||
export function showAnswerInTextEditor(
|
||||
filename: string,
|
||||
range: vscode.Range,
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import { devDataPath } from "core/util/paths";
|
|||
import * as vscode from "vscode";
|
||||
|
||||
import { getMetaKeyLabel, getPlatform } from "../util/util";
|
||||
import { uriFromFilePath } from "../util/vscode";
|
||||
|
||||
import type { VsCodeWebviewProtocol } from "../webviewProtocol";
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import * as vscode from "vscode";
|
||||
|
||||
import { openEditorAndRevealRange, translate } from "./util/vscode";
|
||||
import URI from "uri-js"
|
||||
import URI from "uri-js";
|
||||
|
||||
export interface SuggestionRanges {
|
||||
oldRange: vscode.Range;
|
||||
|
|
@ -15,7 +15,6 @@ export const editorToSuggestions: Map<
|
|||
string, // URI of file
|
||||
SuggestionRanges[]
|
||||
> = new Map();
|
||||
export const editorSuggestionsLocked: Map<string, boolean> = new Map(); // Map from editor URI to whether the suggestions are locked
|
||||
export const currentSuggestion: Map<string, number> = new Map(); // Map from editor URI to index of current SuggestionRanges in editorToSuggestions
|
||||
|
||||
// When tab is reopened, rerender the decorations:
|
||||
|
|
@ -58,8 +57,8 @@ const oldSelDecorationType = vscode.window.createTextEditorDecorationType({
|
|||
export function rerenderDecorations(editorUri: string) {
|
||||
const suggestions = editorToSuggestions.get(editorUri);
|
||||
const idx = currentSuggestion.get(editorUri);
|
||||
const editor = vscode.window.visibleTextEditors.find(
|
||||
(editor) => URI.equal(editor.document.uri.toString(), editorUri)
|
||||
const editor = vscode.window.visibleTextEditors.find((editor) =>
|
||||
URI.equal(editor.document.uri.toString(), editorUri),
|
||||
);
|
||||
if (!suggestions || !editor) {
|
||||
return;
|
||||
|
|
@ -141,6 +140,7 @@ export function suggestionDownCommand() {
|
|||
return;
|
||||
}
|
||||
const editorUri = editor.document.uri.toString();
|
||||
const uriString = editorUri.toString();
|
||||
const suggestions = editorToSuggestions.get(editorUri);
|
||||
const idx = currentSuggestion.get(editorUri);
|
||||
if (!suggestions || idx === undefined) {
|
||||
|
|
@ -300,7 +300,7 @@ export async function rejectSuggestionCommand(
|
|||
}
|
||||
|
||||
export async function showSuggestion(
|
||||
editorUri: string,
|
||||
editorUri: vscode.Uri,
|
||||
range: vscode.Range,
|
||||
suggestion: string,
|
||||
): Promise<boolean> {
|
||||
|
|
|
|||
|
|
@ -8,14 +8,14 @@ import { GitExtension, Repository } from "../otherExtensions/git";
|
|||
import {
|
||||
SuggestionRanges,
|
||||
acceptSuggestionCommand,
|
||||
editorSuggestionsLocked,
|
||||
rejectSuggestionCommand,
|
||||
showSuggestion as showSuggestionInEditor,
|
||||
} from "../suggestions";
|
||||
|
||||
import { getUniqueId, openEditorAndRevealRange } from "./vscode";
|
||||
|
||||
import type { FileEdit, RangeInFile, Thread } from "core";
|
||||
import type { FileEdit, Range, RangeInFile, Thread } from "core";
|
||||
import { isUriWithinDirectory } from "core/util/uri";
|
||||
|
||||
const util = require("node:util");
|
||||
const asyncExec = util.promisify(require("node:child_process").exec);
|
||||
|
|
@ -74,20 +74,17 @@ export class VsCodeIdeUtils {
|
|||
return getUniqueId();
|
||||
}
|
||||
|
||||
// ------------------------------------ //
|
||||
// On message handlers
|
||||
|
||||
private _lastDecorationType: vscode.TextEditorDecorationType | null = null;
|
||||
async highlightCode(rangeInFile: RangeInFile, color: string) {
|
||||
const range = new vscode.Range(
|
||||
rangeInFile.range.start.line,
|
||||
rangeInFile.range.start.character,
|
||||
rangeInFile.range.end.line,
|
||||
rangeInFile.range.end.character,
|
||||
async highlightCode(uri: vscode.Uri, range: Range, color: string) {
|
||||
const vsCodeRange = new vscode.Range(
|
||||
range.start.line,
|
||||
range.start.character,
|
||||
range.end.line,
|
||||
range.end.character,
|
||||
);
|
||||
const editor = await openEditorAndRevealRange(
|
||||
rangeInFile.filepath,
|
||||
range,
|
||||
uri,
|
||||
vsCodeRange,
|
||||
vscode.ViewColumn.One,
|
||||
);
|
||||
if (editor) {
|
||||
|
|
@ -95,11 +92,13 @@ export class VsCodeIdeUtils {
|
|||
backgroundColor: color,
|
||||
isWholeLine: true,
|
||||
});
|
||||
editor.setDecorations(decorationType, [range]);
|
||||
editor.setDecorations(decorationType, [vsCodeRange]);
|
||||
|
||||
const cursorDisposable = vscode.window.onDidChangeTextEditorSelection(
|
||||
(event) => {
|
||||
if (event.textEditor.document.uri.fsPath === rangeInFile.filepath) {
|
||||
if (
|
||||
URI.equal(event.textEditor.document.uri.toString(), uri.toString())
|
||||
) {
|
||||
cursorDisposable.dispose();
|
||||
editor.setDecorations(decorationType, []);
|
||||
}
|
||||
|
|
@ -118,17 +117,16 @@ export class VsCodeIdeUtils {
|
|||
}
|
||||
}
|
||||
|
||||
showSuggestion(edit: FileEdit) {
|
||||
// showSuggestion already exists
|
||||
showSuggestion(uri: vscode.Uri, range: Range, suggestion: string) {
|
||||
showSuggestionInEditor(
|
||||
edit.filepath,
|
||||
uri,
|
||||
new vscode.Range(
|
||||
edit.range.start.line,
|
||||
edit.range.start.character,
|
||||
edit.range.end.line,
|
||||
edit.range.end.character,
|
||||
range.start.line,
|
||||
range.start.character,
|
||||
range.end.line,
|
||||
range.end.character,
|
||||
),
|
||||
edit.replacement,
|
||||
suggestion,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -207,7 +205,7 @@ export class VsCodeIdeUtils {
|
|||
return uri.scheme === "file" || uri.scheme === "vscode-remote";
|
||||
}
|
||||
|
||||
getOpenFiles(): string[] {
|
||||
getOpenFiles(): vscode.Uri[] {
|
||||
return vscode.window.tabGroups.all
|
||||
.map((group) => {
|
||||
return group.tabs.map((tab) => {
|
||||
|
|
@ -216,8 +214,7 @@ export class VsCodeIdeUtils {
|
|||
})
|
||||
.flat()
|
||||
.filter(Boolean) // filter out undefined values
|
||||
.filter((uri) => this.documentIsCode(uri)) // Filter out undesired documents
|
||||
.map((uri) => uri.toString());
|
||||
.filter((uri) => this.documentIsCode(uri)); // Filter out undesired documents
|
||||
}
|
||||
|
||||
getVisibleFiles(): string[] {
|
||||
|
|
@ -232,44 +229,15 @@ export class VsCodeIdeUtils {
|
|||
vscode.window.visibleTextEditors
|
||||
.filter((editor) => this.documentIsCode(editor.document.uri))
|
||||
.forEach((editor) => {
|
||||
if (URI.equal(editor.document.uri, uri.toString())) {
|
||||
if (URI.equal(editor.document.uri.toString(), uri.toString())) {
|
||||
editor.document.save();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private _cachedPath: path.PlatformPath | undefined;
|
||||
get path(): path.PlatformPath {
|
||||
if (this._cachedPath) {
|
||||
return this._cachedPath;
|
||||
}
|
||||
|
||||
// Return "path" module for either windows or posix depending on sample workspace folder path format
|
||||
const sampleWorkspaceFolder =
|
||||
vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
|
||||
const isWindows = sampleWorkspaceFolder
|
||||
? !sampleWorkspaceFolder.startsWith("/")
|
||||
: false;
|
||||
|
||||
this._cachedPath = isWindows ? path.win32 : path.posix;
|
||||
return this._cachedPath;
|
||||
}
|
||||
|
||||
getAbsolutePath(filepath: string): string {
|
||||
const workspaceDirectories = this.getWorkspaceDirectories();
|
||||
if (!this.path.isAbsolute(filepath) && workspaceDirectories.length === 1) {
|
||||
return this.path.join(workspaceDirectories[0], filepath);
|
||||
} else {
|
||||
return filepath;
|
||||
}
|
||||
}
|
||||
|
||||
async readRangeInFile(
|
||||
filepath: string,
|
||||
range: vscode.Range,
|
||||
): Promise<string> {
|
||||
async readRangeInFile(uri: vscode.Uri, range: vscode.Range): Promise<string> {
|
||||
const contents = new TextDecoder().decode(
|
||||
await vscode.workspace.fs.readFile(vscode.Uri.file(filepath)),
|
||||
await vscode.workspace.fs.readFile(uri),
|
||||
);
|
||||
const lines = contents.split("\n");
|
||||
return `${lines
|
||||
|
|
@ -484,11 +452,11 @@ export class VsCodeIdeUtils {
|
|||
async getRepo(forDirectory: vscode.Uri): Promise<Repository | undefined> {
|
||||
const workspaceDirs = this.getWorkspaceDirectories();
|
||||
const parentDir = workspaceDirs.find((dir) =>
|
||||
forDirectory.fsPath.startsWith(dir),
|
||||
isUriWithinDirectory(forDirectory.toString(), dir.toString()),
|
||||
);
|
||||
if (parentDir) {
|
||||
// Check if the repository is already cached
|
||||
const cachedRepo = this.repoCache.get(parentDir);
|
||||
const cachedRepo = this.repoCache.get(parentDir.toString());
|
||||
if (cachedRepo) {
|
||||
return cachedRepo;
|
||||
}
|
||||
|
|
@ -513,15 +481,15 @@ export class VsCodeIdeUtils {
|
|||
|
||||
if (parentDir) {
|
||||
// Cache the repository for the parent directory
|
||||
this.repoCache.set(parentDir, repo);
|
||||
this.repoCache.set(parentDir.toString(), repo);
|
||||
}
|
||||
|
||||
return repo;
|
||||
}
|
||||
|
||||
async getGitRoot(forDirectory: string): Promise<string | undefined> {
|
||||
const repo = await this.getRepo(vscode.Uri.file(forDirectory));
|
||||
return repo?.rootUri?.fsPath;
|
||||
async getGitRoot(forDirectory: vscode.Uri): Promise<vscode.Uri | undefined> {
|
||||
const repo = await this.getRepo(forDirectory);
|
||||
return repo?.rootUri;
|
||||
}
|
||||
|
||||
async getBranch(forDirectory: vscode.Uri) {
|
||||
|
|
@ -556,7 +524,7 @@ export class VsCodeIdeUtils {
|
|||
const diffs: string[] = [];
|
||||
|
||||
for (const dir of this.getWorkspaceDirectories()) {
|
||||
const repo = await this.getRepo(vscode.Uri.file(dir));
|
||||
const repo = await this.getRepo(dir);
|
||||
if (!repo) {
|
||||
continue;
|
||||
}
|
||||
|
|
@ -581,7 +549,7 @@ export class VsCodeIdeUtils {
|
|||
editor.selections.forEach((selection) => {
|
||||
// if (!selection.isEmpty) {
|
||||
rangeInFiles.push({
|
||||
filepath: editor.document.uri.fsPath,
|
||||
filepath: editor.document.uri.toString(),
|
||||
range: {
|
||||
start: {
|
||||
line: selection.start.line,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue