11import * as vscode from 'vscode' ;
22
3- // Variable to store the last pasted content
4- let lastPastedContent : string | null = null ;
5-
63// Flag to track if the selection was auto-selected after pasting
74let autoSelected : boolean = false ;
5+ let manualMouseSelected : boolean = false ;
6+ let manualKeyboardSelected : boolean = false ;
87
98export function activate ( context : vscode . ExtensionContext ) {
109 // Create an output channel for logging extension-related activities
@@ -45,26 +44,27 @@ export function activate(context: vscode.ExtensionContext) {
4544 vscode . commands . registerCommand ( 'type' , ( args ) => {
4645 const editor = vscode . window . activeTextEditor ;
4746 const enableTypeDetection = vscode . workspace . getConfiguration ( 'autoSelectPastedText' ) . get ( 'enableTypeDetection' ) ;
48- const enableManualSelection = vscode . workspace . getConfiguration ( 'autoSelectPastedText' ) . get ( 'enableManualSelection' ) ;
4947
5048 if ( editor ) {
5149 const currentSelection = editor . selection ;
5250
53- if ( autoSelected && enableTypeDetection && ! currentSelection . isEmpty ) {
54- const currentPosition = currentSelection . end ;
55- editor . selection = new vscode . Selection ( currentPosition , currentPosition ) ;
56- autoSelected = false ; // Reset the flag
57- } else if ( ! autoSelected && enableManualSelection && ! currentSelection . isEmpty ) {
51+ if ( enableTypeDetection && autoSelected && ! currentSelection . isEmpty ) {
5852 const currentPosition = currentSelection . end ;
5953 editor . selection = new vscode . Selection ( currentPosition , currentPosition ) ;
6054 }
6155 }
6256 vscode . commands . executeCommand ( 'default:type' , args ) ;
6357 } ) ;
6458
59+
6560 // Register the paste command
6661 let pasteCommandDisposable = vscode . commands . registerCommand ( 'editor.action.clipboardPasteAction' , async ( ) => {
6762 const enableAutoSelection = vscode . workspace . getConfiguration ( 'autoSelectPastedText' ) . get ( 'enableAutoSelection' ) ;
63+ const enableManualSelection = vscode . workspace . getConfiguration ( 'autoSelectPastedText' ) . get ( 'enableManualSelection' ) ;
64+
65+ if ( manualMouseSelected || manualKeyboardSelected ) {
66+ autoSelected = false ;
67+ }
6868
6969 log ( 'Paste command executed.' ) ;
7070
@@ -77,12 +77,11 @@ export function activate(context: vscode.ExtensionContext) {
7777 let targetSelection : vscode . Selection ;
7878
7979 // Determine the target for the paste: either append after the current selection or replace it
80- if ( clipboardContent === lastPastedContent ) {
80+ if ( autoSelected || enableManualSelection ) {
8181 const currentPosition = editor . selection . end ;
8282 targetSelection = new vscode . Selection ( currentPosition , currentPosition ) ;
8383 } else {
8484 targetSelection = editor . selection ;
85- lastPastedContent = clipboardContent ; // Cache the clipboard content
8685 }
8786
8887 // Split content by lines to calculate the selection end position later
@@ -104,7 +103,6 @@ export function activate(context: vscode.ExtensionContext) {
104103 if ( enableAutoSelection ) {
105104 // Adjust the selection to cover the pasted content
106105 editor . selection = new vscode . Selection ( targetSelection . start , endPosition ) ;
107- // Set the autoSelected flag since we've auto-selected the pasted text
108106 autoSelected = true ;
109107 }
110108 // Reveal the pasted content in the editor
@@ -115,19 +113,29 @@ export function activate(context: vscode.ExtensionContext) {
115113 } ) ;
116114 }
117115 }
116+ // At the end of this command, reset the manual selection flags:
117+ manualMouseSelected = false ;
118+ manualKeyboardSelected = false ;
118119 } ) ;
119120
120121 // Add the paste command disposable to the extension context's subscriptions
121122 context . subscriptions . push ( pasteCommandDisposable ) ;
122123
123124 // Debug: Log selection changes in the editor for tracking
124125 vscode . window . onDidChangeTextEditorSelection ( ( event ) => {
126+ if ( event . kind === vscode . TextEditorSelectionChangeKind . Mouse ) {
127+ manualMouseSelected = true ;
128+ } else if ( event . kind === vscode . TextEditorSelectionChangeKind . Keyboard ) {
129+ manualKeyboardSelected = true ;
130+ }
131+
125132 if ( event . textEditor . document . uri . scheme === 'file' ) {
133+ autoSelected = event . kind === 3 ;
126134 const selections = event . selections ;
127135 if ( selections && selections . length > 0 ) {
128136 const selection = selections [ 0 ] ;
129137 log ( `Selection changed: Start(${ selection . start . line } , ${ selection . start . character } ), End(${ selection . end . line } , ${ selection . end . character } )` ) ;
130138 }
131139 }
132140 } ) ;
133- }
141+ }
0 commit comments