11'use strict' ;
22
33import * as path from 'path' ;
4- import { workspace , FileCreateEvent , ExtensionContext , window , TextDocument , SnippetString , commands , Uri } from 'vscode' ;
4+ import { workspace , FileCreateEvent , ExtensionContext , window , TextDocument , SnippetString , commands , Uri , FileRenameEvent , ProgressLocation } from 'vscode' ;
55import { LanguageClient } from 'vscode-languageclient' ;
66import { ListCommandResult } from './buildpath' ;
77import { Commands } from './commands' ;
8+ import { DidRenameFiles } from './protocol' ;
89
910let serverReady : boolean = false ;
1011
@@ -13,16 +14,20 @@ export function setServerStatus(ready: boolean) {
1314}
1415
1516export function registerFileEventHandlers ( client : LanguageClient , context : ExtensionContext , ) {
16- if ( workspace . onDidCreateFiles ) { // Theia doesn't support workspace.onDidCreateFiles yet
17- context . subscriptions . push ( workspace . onDidCreateFiles ( handleNewJavaFiles ) ) ;
18- }
17+ if ( workspace . onDidCreateFiles ) { // Theia doesn't support workspace.onDidCreateFiles yet
18+ context . subscriptions . push ( workspace . onDidCreateFiles ( handleNewJavaFiles ) ) ;
19+ }
20+
21+ if ( workspace . onDidRenameFiles ) {
22+ context . subscriptions . push ( workspace . onDidRenameFiles ( ( e : FileRenameEvent ) => handleRenameFiles ( e , client ) ) ) ;
23+ }
1924}
2025
2126async function handleNewJavaFiles ( e : FileCreateEvent ) {
2227 const emptyFiles : Uri [ ] = [ ] ;
2328 const textDocuments : TextDocument [ ] = [ ] ;
2429 for ( const uri of e . files ) {
25- if ( ! uri . fsPath || ! uri . fsPath . endsWith ( ".java" ) ) {
30+ if ( ! isJavaFile ( uri ) ) {
2631 continue ;
2732 }
2833
@@ -67,6 +72,53 @@ async function handleNewJavaFiles(e: FileCreateEvent) {
6772 }
6873}
6974
75+ async function handleRenameFiles ( e : FileRenameEvent , client : LanguageClient ) {
76+ if ( ! serverReady ) {
77+ return ;
78+ }
79+
80+ const javaRenameEvents : Array < { oldUri : string , newUri : string } > = e . files . filter ( event =>
81+ isJavaFile ( event . oldUri ) && isJavaFile ( event . newUri )
82+ && isInSameDirectory ( event . oldUri , event . newUri )
83+ ) . map ( event => {
84+ return {
85+ oldUri : event . oldUri . toString ( ) ,
86+ newUri : event . newUri . toString ( ) ,
87+ } ;
88+ } ) ;
89+
90+ if ( ! javaRenameEvents . length ) {
91+ return ;
92+ }
93+
94+ window . withProgress ( { location : ProgressLocation . Window } , async ( p ) => {
95+ return new Promise ( async ( resolve , reject ) => {
96+ p . report ( { message : "Computing rename updates..." } ) ;
97+ try {
98+ const edit = await client . sendRequest ( DidRenameFiles . type , {
99+ files : javaRenameEvents
100+ } ) ;
101+ const codeEdit = client . protocol2CodeConverter . asWorkspaceEdit ( edit ) ;
102+ if ( codeEdit ) {
103+ workspace . applyEdit ( codeEdit ) ;
104+ }
105+ } finally {
106+ resolve ( ) ;
107+ }
108+ } ) ;
109+ } ) ;
110+ }
111+
112+ function isJavaFile ( uri : Uri ) : boolean {
113+ return uri . fsPath && uri . fsPath . endsWith ( ".java" ) ;
114+ }
115+
116+ function isInSameDirectory ( oldUri : Uri , newUri : Uri ) : boolean {
117+ const oldDir = path . dirname ( oldUri . fsPath ) ;
118+ const newDir = path . dirname ( newUri . fsPath ) ;
119+ return ! path . relative ( oldDir , newDir ) ;
120+ }
121+
70122function resolveTypeName ( filePath : string ) : string {
71123 const fileName : string = path . basename ( filePath ) ;
72124 const extName : string = path . extname ( fileName ) ;
0 commit comments