1+ 'use strict' ;
2+
3+ import { commands , ExtensionContext , Position , Range , Selection , window } from 'vscode' ;
4+ import { Commands } from './commands' ;
5+ import { getJavaConfiguration } from './utils' ;
6+
7+ let oldPosition : Position = null ;
8+ let newPosition : Position = null ;
9+
10+ export function registerSmartSemicolonDetection ( context : ExtensionContext ) {
11+ context . subscriptions . push ( commands . registerCommand ( Commands . SMARTSEMICOLON_DETECTION_CMD , async ( ) => {
12+ if ( ! didSmartSemicolonInsertion ( ) && enabled ( ) && window . activeTextEditor ! . document . fileName . endsWith ( ".java" ) ) {
13+ const params : SmartDetectionParams = {
14+ uri : window . activeTextEditor . document . uri . toString ( ) ,
15+ position : window . activeTextEditor ! . selection . active ,
16+ } ;
17+ const response : SmartDetectionParams = await commands . executeCommand ( Commands . EXECUTE_WORKSPACE_COMMAND , Commands . SMARTSEMICOLON_DETECTION , JSON . stringify ( params ) ) ;
18+ if ( response !== null ) {
19+ window . activeTextEditor ! . edit ( editBuilder => {
20+ oldPosition = window . activeTextEditor ! . selection . active ;
21+ editBuilder . insert ( response . position , ";" ) ;
22+ window . activeTextEditor . selections = [ new Selection ( response . position , response . position ) ] ;
23+ newPosition = window . activeTextEditor ! . selection . active ;
24+ } ) ;
25+ return ;
26+ }
27+ }
28+ window . activeTextEditor ! . edit ( editBuilder => {
29+ editBuilder . insert ( window . activeTextEditor ! . selection . active , ";" ) ;
30+ } ) ;
31+ newPosition = null ;
32+ oldPosition = null ;
33+ } ) ) ;
34+ context . subscriptions . push ( commands . registerCommand ( Commands . SMARTSEMICOLON_DETECTION_UNDO , async ( ) => {
35+ if ( didSmartSemicolonInsertion ( ) && enabled ( ) ) {
36+ window . activeTextEditor ! . edit ( editBuilder => {
37+ editBuilder . insert ( oldPosition , ";" ) ;
38+ const delRange = new Range ( newPosition , new Position ( newPosition . line , newPosition . character + 1 ) ) ;
39+ editBuilder . delete ( delRange ) ;
40+ window . activeTextEditor . selections = [ new Selection ( oldPosition , oldPosition ) ] ;
41+ oldPosition = null ;
42+ newPosition = null ;
43+ } ) ;
44+ return ;
45+ }
46+ window . activeTextEditor ! . edit ( ( ) => {
47+ commands . executeCommand ( "deleteLeft" ) ;
48+ } ) ;
49+ oldPosition = null ;
50+ newPosition = null ;
51+ } ) ) ;
52+ }
53+
54+ interface SmartDetectionParams {
55+ uri : String ;
56+ position : Position ;
57+ }
58+
59+ function didSmartSemicolonInsertion ( ) {
60+ const smartSemicolonInsertion = window . activeTextEditor . selections . length === 1 && enabled ( ) && oldPosition !== null && newPosition !== null ;
61+ if ( smartSemicolonInsertion ) {
62+ const active = window . activeTextEditor ! . selection . active ;
63+ const prev = new Position ( active . line , active . character === 0 ? 0 : active . character - 1 ) ;
64+ return newPosition . isEqual ( prev ) ;
65+ }
66+ return smartSemicolonInsertion ;
67+ }
68+
69+ function enabled ( ) {
70+ return getJavaConfiguration ( ) . get < boolean > ( "edit.smartSemicolonDetection.enabled" ) ;
71+ }
72+
73+ export function setSmartSemiColonDetectionState ( oldPos : Position , newPos : Position ) {
74+ oldPosition = oldPos ;
75+ newPos = newPos ;
76+ }
0 commit comments