|
import * as fs from "fs";
|
|
import * as os from "os";
|
|
import * as path from "path";
|
|
import * as vscode from "vscode";
|
|
|
|
interface CursorSettings {
|
|
settings: any;
|
|
keybindings: any;
|
|
extensions: string[];
|
|
workspaceState: any;
|
|
}
|
|
|
|
export function activate(context: vscode.ExtensionContext) {
|
|
// Original ignore/focus commands
|
|
let ignoreDisposable = vscode.commands.registerCommand(
|
|
"cursor.wrapWithIgnore",
|
|
() => {
|
|
const editor = vscode.window.activeTextEditor;
|
|
if (editor) {
|
|
const selection = editor.selection;
|
|
const text = editor.document.getText(selection);
|
|
|
|
editor.edit((editBuilder) => {
|
|
editBuilder.replace(
|
|
selection,
|
|
`<!-- cursor-ignore -->\n${text}\n<!-- cursor-ignore-end -->`
|
|
);
|
|
});
|
|
}
|
|
}
|
|
);
|
|
|
|
let focusDisposable = vscode.commands.registerCommand(
|
|
"cursor.wrapWithFocus",
|
|
() => {
|
|
const editor = vscode.window.activeTextEditor;
|
|
if (editor) {
|
|
const selection = editor.selection;
|
|
const text = editor.document.getText(selection);
|
|
|
|
editor.edit((editBuilder) => {
|
|
editBuilder.replace(
|
|
selection,
|
|
`<!-- cursor-focus -->\n${text}\n<!-- cursor-focus-end -->`
|
|
);
|
|
});
|
|
}
|
|
}
|
|
);
|
|
|
|
// New settings sync commands
|
|
let exportDisposable = vscode.commands.registerCommand(
|
|
"cursor.exportSettings",
|
|
async () => {
|
|
try {
|
|
await exportCursorSettings();
|
|
vscode.window.showInformationMessage(
|
|
"Cursor settings exported successfully"
|
|
);
|
|
} catch (error) {
|
|
vscode.window.showErrorMessage(`Failed to export settings: ${error}`);
|
|
}
|
|
}
|
|
);
|
|
|
|
let importDisposable = vscode.commands.registerCommand(
|
|
"cursor.importSettings",
|
|
async () => {
|
|
try {
|
|
await importCursorSettings();
|
|
vscode.window.showInformationMessage(
|
|
"Cursor settings imported successfully"
|
|
);
|
|
} catch (error) {
|
|
vscode.window.showErrorMessage(`Failed to import settings: ${error}`);
|
|
}
|
|
}
|
|
);
|
|
|
|
let syncDisposable = vscode.commands.registerCommand(
|
|
"cursor.syncSettings",
|
|
async () => {
|
|
try {
|
|
await exportCursorSettings();
|
|
await importCursorSettings();
|
|
vscode.window.showInformationMessage(
|
|
"Cursor settings synced successfully"
|
|
);
|
|
} catch (error) {
|
|
vscode.window.showErrorMessage(`Failed to sync settings: ${error}`);
|
|
}
|
|
}
|
|
);
|
|
|
|
context.subscriptions.push(
|
|
ignoreDisposable,
|
|
focusDisposable,
|
|
exportDisposable,
|
|
importDisposable,
|
|
syncDisposable
|
|
);
|
|
}
|
|
|
|
async function exportCursorSettings(): Promise<void> {
|
|
const globalStoragePath = path.join(os.homedir(), ".cursor-global");
|
|
|
|
// Ensure global storage exists
|
|
if (!fs.existsSync(globalStoragePath)) {
|
|
fs.mkdirSync(globalStoragePath, { recursive: true });
|
|
}
|
|
|
|
const settings: CursorSettings = {
|
|
settings: vscode.workspace.getConfiguration("cursor"),
|
|
keybindings: await getKeybindings(),
|
|
extensions: await getInstalledExtensions(),
|
|
workspaceState: await getWorkspaceState(),
|
|
};
|
|
|
|
fs.writeFileSync(
|
|
path.join(globalStoragePath, "cursor-settings.json"),
|
|
JSON.stringify(settings, null, 2)
|
|
);
|
|
}
|
|
|
|
async function importCursorSettings(): Promise<void> {
|
|
const globalStoragePath = path.join(os.homedir(), ".cursor-global");
|
|
const settingsPath = path.join(globalStoragePath, "cursor-settings.json");
|
|
|
|
if (!fs.existsSync(settingsPath)) {
|
|
throw new Error("No settings file found");
|
|
}
|
|
|
|
const settings: CursorSettings = JSON.parse(
|
|
fs.readFileSync(settingsPath, "utf8")
|
|
);
|
|
|
|
// Apply settings
|
|
await vscode.workspace
|
|
.getConfiguration("cursor")
|
|
.update("", settings.settings, true);
|
|
await setKeybindings(settings.keybindings);
|
|
await installExtensions(settings.extensions);
|
|
await setWorkspaceState(settings.workspaceState);
|
|
}
|
|
|
|
async function getKeybindings(): Promise<any> {
|
|
const keybindingsPath = path.join(
|
|
os.homedir(),
|
|
".cursor",
|
|
"User",
|
|
"keybindings.json"
|
|
);
|
|
if (fs.existsSync(keybindingsPath)) {
|
|
return JSON.parse(fs.readFileSync(keybindingsPath, "utf8"));
|
|
}
|
|
return {};
|
|
}
|
|
|
|
async function setKeybindings(keybindings: any): Promise<void> {
|
|
const keybindingsPath = path.join(
|
|
os.homedir(),
|
|
".cursor",
|
|
"User",
|
|
"keybindings.json"
|
|
);
|
|
fs.writeFileSync(keybindingsPath, JSON.stringify(keybindings, null, 2));
|
|
}
|
|
|
|
async function getInstalledExtensions(): Promise<string[]> {
|
|
return vscode.extensions.all
|
|
.filter((ext) => !ext.packageJSON.isBuiltin)
|
|
.map((ext) => ext.id);
|
|
}
|
|
|
|
async function installExtensions(extensions: string[]): Promise<void> {
|
|
for (const ext of extensions) {
|
|
try {
|
|
await vscode.commands.executeCommand(
|
|
"workbench.extensions.installExtension",
|
|
ext
|
|
);
|
|
} catch (error) {
|
|
console.error(`Failed to install extension ${ext}:`, error);
|
|
}
|
|
}
|
|
}
|
|
|
|
async function getWorkspaceState(): Promise<any> {
|
|
const workspacePath = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
|
|
if (!workspacePath) return {};
|
|
|
|
const statePath = path.join(workspacePath, ".cursor", "workspace-state.json");
|
|
if (fs.existsSync(statePath)) {
|
|
return JSON.parse(fs.readFileSync(statePath, "utf8"));
|
|
}
|
|
return {};
|
|
}
|
|
|
|
async function setWorkspaceState(state: any): Promise<void> {
|
|
const workspacePath = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
|
|
if (!workspacePath) return;
|
|
|
|
const cursorPath = path.join(workspacePath, ".cursor");
|
|
if (!fs.existsSync(cursorPath)) {
|
|
fs.mkdirSync(cursorPath, { recursive: true });
|
|
}
|
|
|
|
fs.writeFileSync(
|
|
path.join(cursorPath, "workspace-state.json"),
|
|
JSON.stringify(state, null, 2)
|
|
);
|
|
}
|