2023-09-14 23:22:19 +02:00
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode' ;
2023-12-24 15:04:34 +01:00
import * as codemp from '../index' ; // TODO why won't it work with a custom name???
var CACHE = new codemp . OpCache ( ) ;
var BUFFERS : string [ ] [ ] = [ ] ;
2023-11-19 00:58:18 +01:00
let smallNumberDecorationType = vscode . window . createTextEditorDecorationType ( { } ) ;
2023-09-14 23:22:19 +02:00
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export function activate ( context : vscode.ExtensionContext ) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console . log ( 'Congratulations, your extension "codempvscode" is now active!' ) ;
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode . commands . registerCommand ( 'codempvscode.helloWorld' , ( ) = > {
// The code you place here will be executed every time your command is executed
// Display a message box to the user
vscode . window . showInformationMessage ( process . cwd ( ) ) ;
} ) ;
let connectCommand = vscode . commands . registerCommand ( 'codempvscode.connect' , connect ) ;
let joinCommand = vscode . commands . registerCommand ( 'codempvscode.join' , join ) ;
2023-10-01 10:54:08 +02:00
let attachCommand = vscode . commands . registerCommand ( 'codempvscode.attach' , attach ) ;
2023-10-01 10:51:28 +02:00
let createBufferCommand = vscode . commands . registerCommand ( 'codempvscode.createBuffer' , createBuffer ) ;
2023-10-02 23:35:19 +02:00
let disconnectBufferCommand = vscode . commands . registerCommand ( 'codempvscode.disconnectBuffer' , disconnectBuffer ) ;
2023-12-22 19:39:29 +01:00
let syncBufferCommand = vscode . commands . registerCommand ( 'codempvscode.sync' , sync ) ;
2023-09-14 23:22:19 +02:00
context . subscriptions . push ( connectCommand ) ;
context . subscriptions . push ( joinCommand ) ;
2023-10-01 10:54:08 +02:00
context . subscriptions . push ( attachCommand ) ;
2023-10-01 10:51:28 +02:00
context . subscriptions . push ( createBufferCommand ) ;
2023-12-22 19:39:29 +01:00
context . subscriptions . push ( disconnectBufferCommand ) ;
context . subscriptions . push ( syncBufferCommand ) ;
2023-09-14 23:22:19 +02:00
context . subscriptions . push ( disposable ) ;
2023-12-22 19:39:29 +01:00
2023-09-14 23:22:19 +02:00
}
async function connect() {
2023-11-19 00:58:18 +01:00
let host = await vscode . window . showInputBox ( { prompt : "server host (default to http://alemi.dev:50052)" } )
2023-09-14 23:22:19 +02:00
if ( host === undefined ) return // user cancelled with ESC
2023-11-19 00:58:18 +01:00
if ( host . length == 0 ) host = "http://alemi.dev:50052"
2023-09-14 23:22:19 +02:00
await codemp . connect ( host ) ;
2023-10-02 23:35:19 +02:00
vscode . window . showInformationMessage ( ` Connected to codemp @[ ${ host } ] ` ) ;
2023-09-14 23:22:19 +02:00
}
async function join() {
2023-10-01 10:47:53 +02:00
let workspace = await vscode . window . showInputBox ( { prompt : "workspace to attach (default to default)" } ) ;
let buffer : string = ( await vscode . window . showInputBox ( { prompt : "buffer name for the file needed to update other clients cursors" } ) ) ! ;
let editor = vscode . window . activeTextEditor ;
//let editor = activeEditor.document.getText();
//let doc = editor?.document;
2023-09-14 23:22:19 +02:00
if ( workspace === undefined ) return // user cancelled with ESC
if ( workspace . length == 0 ) workspace = "default"
if ( buffer === undefined ) return // user cancelled with ESC
2023-12-22 19:39:29 +01:00
if ( buffer . length == 0 ) { workspace = "default" ; buffer = "fucl" ; }
2023-10-01 10:47:53 +02:00
2023-09-14 23:22:19 +02:00
let controller = await codemp . join ( workspace )
2023-10-01 10:47:53 +02:00
try {
controller . callback ( ( event :any ) = > {
try {
//console.log(event);
let curPos = vscode . window . activeTextEditor ? . selection . active ;
let PosNumber = curPos ? . line as number ;
let posizione = new vscode . Position ( 0 , PosNumber ) ;
//console.log("posizione", posizione, "\n");
let range_start = new vscode . Position ( event . start . row , event . start . col ) ; // -1?
let range_end = new vscode . Position ( event . end . row , event . end . col ) ; // -1? idk if this works it's kinda funny, should test with someone with a working version of codemp
/ * c o n s o l e . l o g ( " r a n g e _ s t a r t " , r a n g e _ s t a r t , " \ n " ) ;
console . log ( "range_end" , range_end , "\n" ) ; * /
const decorationRange = new vscode . Range ( range_start , range_end ) ;
2023-11-19 00:58:18 +01:00
smallNumberDecorationType . dispose ( ) ;
smallNumberDecorationType = vscode . window . createTextEditorDecorationType ( { //should remove the highlighted text after a while
2023-10-01 10:47:53 +02:00
borderWidth : '5px' ,
borderStyle : 'solid' ,
2023-10-02 23:35:19 +02:00
overviewRulerColor : 'blue' ,
2023-10-01 10:47:53 +02:00
overviewRulerLane : vscode.OverviewRulerLane.Right ,
light : {
// this color will be used in light color themes
2023-10-02 23:35:19 +02:00
borderColor : 'darkblue' //should create this color based on event.user (uuid)
2023-10-01 10:47:53 +02:00
} ,
dark : {
// this color will be used in dark color themes
2023-10-02 23:35:19 +02:00
borderColor : 'lightblue' //should create this color based on event.user (uuid)
2023-10-01 10:47:53 +02:00
}
} ) ;
//let DECORATION = vscode.window.createTextEditorDecorationType({backgroundColor: 'red', color: 'white'});
/ * c o n s o l e . l o g ( " E d i t o r " , e d i t o r , " \ n " ) ;
console . log ( "Decoration range " , [ decorationRange ] , "\n" ) ; * /
editor ? . setDecorations ( smallNumberDecorationType , [ decorationRange ] ) ;
} catch ( e ) {
console . log ( "Error" , e , "\n" ) ;
}
2023-09-14 23:22:19 +02:00
} ) ;
2023-10-01 10:47:53 +02:00
} catch ( e ) {
console . log ( "Error" , e , "\n" ) ;
}
2023-09-14 23:22:19 +02:00
2023-11-19 00:58:18 +01:00
vscode . window . onDidChangeTextEditorSelection ( ( event : vscode.TextEditorSelectionChangeEvent ) = > {
2023-10-01 10:47:53 +02:00
if ( event . kind == vscode . TextEditorSelectionChangeKind . Command ) return ; // TODO commands might move cursor too
2023-09-14 23:22:19 +02:00
let buf = event . textEditor . document . uri . toString ( )
let selection = event . selections [ 0 ] // TODO there may be more than one cursor!!
//let anchor = [selection.anchor.line+1, selection.anchor.character]
//let position = [selection.active.line+1, selection.active.character+1]
2023-10-01 10:47:53 +02:00
let anchor : [ number , number ] = [ selection . anchor . line , selection . anchor . character ] ;
let position : [ number , number ] = [ selection . active . line , selection . active . character + 1 ] ;
/ * c o n s o l e . l o g ( " B u f f e r f r o m s e l e c t i o n " + b u f f e r + " \ n " ) ;
2023-09-14 23:22:19 +02:00
console . log ( "selection " + selection + "\n" ) ;
console . log ( "Anchor selection" + anchor + "\n" ) ;
2023-10-01 10:47:53 +02:00
console . log ( "position selection" + position + "\n" ) ; * /
2023-09-14 23:22:19 +02:00
controller . send ( buffer , anchor , position ) ;
} ) ;
vscode . window . showInformationMessage ( ` Connected to workspace @[ ${ workspace } ] ` ) ;
}
2023-11-19 00:58:18 +01:00
async function createBuffer() {
2023-10-02 23:35:19 +02:00
let workspace = "test" ; //ask which workspace
let buffer : any = ( await vscode . window . showInputBox ( { prompt : "path of the buffer to create" } ) ) ! ;
2023-10-01 10:51:28 +02:00
console . log ( "new buffer created " , buffer , "\n" ) ;
codemp . create ( buffer ) ;
console . log ( "new createdBuffer " , createBuffer , "\n" ) ;
2023-10-02 23:35:19 +02:00
let editor = vscode . window . activeTextEditor ;
if ( editor === undefined ) { return } // TODO say something!!!!!!
let range = new vscode . Range (
editor . document . positionAt ( 0 ) ,
editor . document . positionAt ( editor . document . getText ( ) . length )
)
buffer = await codemp . attach ( workspace ) ;
buffer . delta ( range . start , editor . document . getText ( ) , range . end ) ; //test it plz coded this at 10am :(
//Should i disconnect or stay attached to buffer???
2023-10-01 10:51:28 +02:00
}
2023-10-01 10:47:53 +02:00
2023-10-01 10:54:08 +02:00
async function attach() {
2023-12-22 19:39:29 +01:00
let buffer_name : any = ( await vscode . window . showInputBox ( { prompt : "buffer to attach to" } ) ) ! ;
let buffer = await codemp . attach ( buffer_name ) ;
console . log ( "attached to buffer" , buffer_name ) ;
console . log ( "buffer" , buffer ) ;
2023-10-01 10:54:08 +02:00
2023-12-24 15:04:34 +01:00
// let fileUri = buffer_name;
// const fileName = 'untitled-1';
// const newFileUri = vscode.Uri.file(fileName).with({ scheme: 'untitled', path: fileName });
// vscode.workspace.openTextDocument()
// await vscode.workspace.openTextDocument(newFileUri);
// vscode.commands.executeCommand('vscode.open', newFileUri);
2023-10-01 10:54:08 +02:00
let editor = vscode . window . activeTextEditor ;
2023-12-22 19:39:29 +01:00
if ( editor === undefined ) {
vscode . window . showInformationMessage ( ` Open a file first ` ) ;
return ;
}
editor = vscode . window . activeTextEditor ! ;
2023-10-01 10:54:08 +02:00
console . log ( "Buffer = " , buffer , "\n" ) ;
2023-12-22 19:39:29 +01:00
vscode . window . showInformationMessage ( ` Connected to codemp workspace buffer @[ ${ buffer_name } ] ` ) ;
let file_uri = editor . document . uri ;
BUFFERS . push ( [ file_uri , buffer_name ] ) ;
2023-10-01 10:54:08 +02:00
2023-11-19 00:58:18 +01:00
vscode . workspace . onDidChangeTextDocument ( ( event :vscode.TextDocumentChangeEvent ) = > {
2023-10-01 10:56:19 +02:00
console . log ( event . reason ) ;
2023-12-22 19:39:29 +01:00
if ( event . document . uri != file_uri ) return ; // ?
2023-10-01 10:56:19 +02:00
for ( let change of event . contentChanges ) {
2023-12-24 15:04:34 +01:00
if ( CACHE . get ( buffer_name , change . rangeOffset , change . text , change . rangeOffset + change . rangeLength ) ) continue ;
2023-11-19 00:58:18 +01:00
buffer . send ( {
span : {
start : change.rangeOffset ,
end : change.rangeOffset + change . rangeLength
} ,
content : change.text
} ) ;
2023-10-01 10:56:19 +02:00
}
} ) ;
2023-11-19 00:58:18 +01:00
//await new Promise((resolve) => setTimeout(resolve, 200)); // tonioware
console . log ( "test" ) ;
2023-10-01 10:56:19 +02:00
2023-11-19 00:58:18 +01:00
buffer . callback ( ( event : any ) = > {
2023-12-24 15:04:34 +01:00
CACHE . put ( buffer_name , event . span . start , event . content , event . span . end ) ; //what's the difference between e.text and e.content like it's on lib.rs?
2023-10-01 10:55:27 +02:00
if ( editor === undefined ) { return } // TODO say something!!!!!!
let range = new vscode . Range (
editor . document . positionAt ( event . span . start ) ,
editor . document . positionAt ( event . span . end )
)
2023-12-24 15:04:34 +01:00
editor . edit ( editBuilder = > {
editBuilder
. replace ( range , event . content )
} )
2023-10-01 10:55:27 +02:00
} ) ;
2023-10-01 10:54:08 +02:00
}
2023-11-19 00:58:18 +01:00
async function disconnectBuffer() {
2023-10-02 23:35:19 +02:00
let buffer : string = ( await vscode . window . showInputBox ( { prompt : "buffer name for the file to disconnect from" } ) ) ! ;
2023-12-24 15:04:34 +01:00
codemp . disconnectBuffer ( buffer ) ;
2023-10-02 23:35:19 +02:00
vscode . window . showInformationMessage ( ` Disconnected from codemp workspace buffer @[ ${ buffer } ] ` ) ;
}
2023-12-24 15:04:34 +01:00
async function sync() {
2023-12-22 19:39:29 +01:00
let editor = vscode . window . activeTextEditor ;
if ( editor === undefined ) { return }
for ( let tuple of BUFFERS ) {
2023-12-24 15:04:34 +01:00
console . log ( tuple [ 0 ] ) ;
console . log ( "\n" ) ;
console . log ( editor ? . document . uri . toString ( ) ) ;
if ( tuple [ 0 ] === editor ? . document . uri . toString ( ) ) {
2023-12-22 19:39:29 +01:00
2023-12-24 15:04:34 +01:00
let buffer = await codemp . getBuffer ( tuple [ 1 ] ) ;
2023-12-22 19:39:29 +01:00
if ( buffer == null ) {
vscode . window . showErrorMessage ( "This buffer does not exist anymore" ) ;
return ;
}
let range = new vscode . Range (
editor . document . positionAt ( 0 ) ,
editor . document . positionAt ( editor . document . getText ( ) . length )
2023-12-24 15:04:34 +01:00
) ;
let content = buffer . content ( ) ;
CACHE . put ( tuple [ 1 ] , 0 , content , editor . document . getText ( ) . length ) ;
editor . edit ( editBuilder = > editBuilder . replace ( range , content ) ) ;
2023-12-22 19:39:29 +01:00
return ;
}
}
vscode . window . showErrorMessage ( "This buffer is not managed by codemp" ) ;
}
2023-10-01 10:47:53 +02:00
2023-09-14 23:22:19 +02:00
// This method is called when your extension is deactivated
2023-10-02 23:35:19 +02:00
export function deactivate() {
//Maybe i should disconnect from every workspace and buffer ??? // TODO
}
2023-11-19 00:58:18 +01:00