forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceUtils.java
More file actions
187 lines (164 loc) · 5.35 KB
/
Copy pathResourceUtils.java
File metadata and controls
187 lines (164 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4j.WorkspaceFolder;
import com.microsoft.copilot.eclipse.core.utils.FileUtils;
/**
* Utilities for handling Copilot reference resources in Eclipse UI.
* Supports selection validation, resource adaptation, and statistics collection.
*/
public final class ResourceUtils {
private ResourceUtils() {
// prevent instantiation
}
/**
* Collect valid resources from the selection.
*/
public static List<IResource> collectValidResources(IStructuredSelection selection) {
List<IResource> validResources = new ArrayList<>();
if (selection == null || selection.isEmpty()) {
return validResources;
}
for (Object obj : selection.toList()) {
IResource resource = adaptToResource(obj);
if (isValidResource(resource)) {
validResources.add(resource);
}
}
return validResources;
}
/**
* Analyze the selection and return statistics about files, folders, and invalid resources.
*/
public static SelectionStats analyzeSelection(IStructuredSelection selection) {
int fileCount = 0;
int folderCount = 0;
int invalidCount = 0;
if (selection != null && !selection.isEmpty()) {
for (Object obj : selection.toList()) {
IResource resource = adaptToResource(obj);
if (resource instanceof IFile file) {
if (isValidFile(file)) {
fileCount++;
} else {
invalidCount++;
}
} else if (resource instanceof IFolder) {
folderCount++;
} else {
invalidCount++;
}
}
}
return new SelectionStats(fileCount, folderCount, invalidCount);
}
/**
* Derive workspace folders from the given list of resources by extracting their parent projects.
* Returns an unmodifiable list with distinct, accessible workspace folders.
* If the input list is <code>null</code> or empty, returns an empty list.
*
* @param resources list of resources from which their parent projects are used to derive the workspace folders.
* @return a never <code>null</code> list of workspace folders derived from the given resources.
*/
public static List<WorkspaceFolder> deriveWorkspaceFoldersFrom(List<IResource> resources) {
if (resources == null || resources.isEmpty()) {
return List.of();
}
return resources.stream()
.filter(Objects::nonNull)
.map(IResource::getProject)
.filter(Objects::nonNull)
.distinct()
.filter(IProject::isAccessible)
.map(LSPEclipseUtils::toWorkspaceFolder)
.toList();
}
/**
* Adapt an object to IResource.
*/
private static IResource adaptToResource(Object obj) {
if (obj instanceof IResource r) {
return r;
}
if (obj instanceof IAdaptable a) {
IResource r = a.getAdapter(IResource.class);
if (r != null) {
return r;
}
}
return (IResource) Platform.getAdapterManager().getAdapter(obj, IResource.class);
}
/**
* Check if the file is valid (not excluded from referenced files).
*/
private static boolean isValidFile(IFile file) {
return file != null && !FileUtils.isExcludedFromReferencedFiles(file);
}
/**
* Check if the resource is a valid folder or file.
*/
private static boolean isValidResource(IResource resource) {
if (resource instanceof IFolder) {
return true;
}
if (resource instanceof IFile file) {
return isValidFile(file);
}
return false;
}
/**
* Statistics about the selection of resources.
*/
public static final class SelectionStats {
public final int fileCount;
public final int folderCount;
public final int invalidCount;
/**
* Constructor for SelectionStats.
*
* @param fileCount the count of valid files
* @param folderCount the count of folders
* @param invalidCount the count of invalid resources
*/
public SelectionStats(int fileCount, int folderCount, int invalidCount) {
this.fileCount = fileCount;
this.folderCount = folderCount;
this.invalidCount = invalidCount;
}
/**
* Check if the selection has only files.
*
* @return true if there are only files, false otherwise
*/
public boolean hasOnlyFiles() {
return fileCount > 0 && folderCount == 0 && invalidCount == 0;
}
/**
* Check if the selection has only folders.
*
* @return true if there are only folders, false otherwise
*/
public boolean hasOnlyFolders() {
return folderCount > 0 && fileCount == 0 && invalidCount == 0;
}
/**
* Check if the selection has only valid resources (files or folders).
*
* @return true if there are valid resources, false if all are invalid
*/
public boolean hasOnlyValidResources() {
return invalidCount == 0 && (fileCount > 0 || folderCount > 0);
}
}
}