33
44package com .microsoft .copilot .eclipse .ui .chat .tools ;
55
6+ import java .io .File ;
7+ import java .net .URI ;
8+ import java .util .ArrayList ;
69import java .util .HashMap ;
710import java .util .List ;
811import java .util .Map ;
912import java .util .concurrent .CompletableFuture ;
1013
1114import org .apache .commons .lang3 .StringUtils ;
12- import org .eclipse .core .resources .IProject ;
1315import org .eclipse .core .resources .IResource ;
14- import org .eclipse .core . runtime . IPath ;
16+ import org .eclipse .lsp4j . WorkspaceFolder ;
1517
1618import com .microsoft .copilot .eclipse .core .lsp .protocol .ConfirmationMessages ;
1719import com .microsoft .copilot .eclipse .core .lsp .protocol .InputSchema ;
2729import com .microsoft .copilot .eclipse .ui .chat .ChatView ;
2830import com .microsoft .copilot .eclipse .ui .chat .services .ChatServiceManager ;
2931import com .microsoft .copilot .eclipse .ui .chat .services .ReferencedFileService ;
32+ import com .microsoft .copilot .eclipse .ui .utils .ResourceUtils ;
3033import com .microsoft .copilot .eclipse .ui .utils .UiUtils ;
3134
3235/**
@@ -207,29 +210,25 @@ public CompletableFuture<LanguageModelToolResult[]> invoke(Map<String, Object> i
207210 }
208211
209212 static String resolveWorkingDirectoryFromResources (List <IResource > resources ) {
210- if (resources == null ) {
211- return "" ;
212- }
213- for (IResource resource : resources ) {
214- String location = resolveProjectLocation (resource );
215- if (StringUtils .isNotBlank (location )) {
216- return location ;
217- }
218- }
219- return "" ;
213+ return ResourceUtils .deriveWorkspaceFoldersFrom (resources ).stream ()
214+ .findFirst ()
215+ .map (RunInTerminalToolAdapter ::toLocalPath )
216+ .orElse ("" );
220217 }
221218
222219 private static String resolveWorkingDirectory () {
223220 ChatServiceManager manager = CopilotUi .getPlugin () != null ? CopilotUi .getPlugin ().getChatServiceManager () : null ;
224221 if (manager != null ) {
225222 ReferencedFileService fileService = manager .getReferencedFileService ();
226223 if (fileService != null ) {
227- String currentFileLocation = resolveProjectLocation ( fileService . getCurrentFile () );
228- if (StringUtils . isNotBlank ( currentFileLocation ) ) {
229- return currentFileLocation ;
224+ List < IResource > resources = new ArrayList <>( );
225+ if (fileService . getCurrentFile () != null ) {
226+ resources . add ( fileService . getCurrentFile ()) ;
230227 }
231-
232- String referencedLocation = resolveWorkingDirectoryFromResources (fileService .getReferencedFiles ());
228+ if (fileService .getReferencedFiles () != null ) {
229+ resources .addAll (fileService .getReferencedFiles ());
230+ }
231+ String referencedLocation = resolveWorkingDirectoryFromResources (resources );
233232 if (StringUtils .isNotBlank (referencedLocation )) {
234233 return referencedLocation ;
235234 }
@@ -239,19 +238,15 @@ private static String resolveWorkingDirectory() {
239238 return "" ;
240239 }
241240
242- private static String resolveProjectLocation ( IResource resource ) {
243- if (resource == null ) {
241+ private static String toLocalPath ( WorkspaceFolder workspaceFolder ) {
242+ if (workspaceFolder == null || StringUtils . isBlank ( workspaceFolder . getUri ()) ) {
244243 return "" ;
245244 }
246- return resolveProjectLocation (resource .getProject ());
247- }
248-
249- private static String resolveProjectLocation (IProject project ) {
250- if (project == null || !project .isAccessible ()) {
245+ try {
246+ return new File (URI .create (workspaceFolder .getUri ())).getPath ();
247+ } catch (IllegalArgumentException e ) {
251248 return "" ;
252249 }
253- IPath location = project .getLocation ();
254- return location != null ? location .toOSString () : "" ;
255250 }
256251
257252 /**
0 commit comments