Skip to content

Commit 3519ab9

Browse files
committed
coderabbitai suggestions and code cleanup
1 parent b7fd36d commit 3519ab9

File tree

1 file changed

+94
-57
lines changed

1 file changed

+94
-57
lines changed

bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/gcov/GcovFileView.java

Lines changed: 94 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.eclipse.core.resources.IWorkspaceRoot;
2525
import org.eclipse.core.resources.ResourcesPlugin;
2626
import org.eclipse.core.runtime.CoreException;
27+
import org.eclipse.core.runtime.IPath;
2728
import org.eclipse.core.runtime.Path;
2829
import org.eclipse.jface.action.Action;
2930
import org.eclipse.jface.action.IToolBarManager;
@@ -53,6 +54,7 @@
5354
import com.espressif.idf.core.logging.Logger;
5455
import com.espressif.idf.core.util.GcovUtility;
5556
import com.espressif.idf.core.util.IDFUtil;
57+
import com.espressif.idf.core.util.StringUtil;
5658

5759
/**
5860
* Gcov file view that can be opened by right clicking on the project. It is used to show the gcno/gcda files as one
@@ -121,6 +123,14 @@ public void run()
121123
refreshList();
122124
}
123125
};
126+
Action selectProjectAction = new Action("Select Project")
127+
{
128+
public void run()
129+
{
130+
openProjectSelectionDialog();
131+
refreshList();
132+
}
133+
};
124134
mgr.add(refreshAction);
125135

126136
// Initial population of the list
@@ -139,14 +149,55 @@ public void selectionChanged(IWorkbenchPart part, ISelection selection)
139149
setSelectedProject((IProject) obj);
140150
}
141151
}
142-
143-
private void refreshList()
152+
153+
private void openProjectSelectionDialog()
144154
{
145-
for (TableItem item : table.getItems())
155+
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
156+
ElementListSelectionDialog dialog = new ElementListSelectionDialog(
157+
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
158+
new org.eclipse.ui.model.WorkbenchLabelProvider());
159+
dialog.setElements(root.getProjects());
160+
dialog.setTitle(Messages.Dialog_SelectProject_Title);
161+
162+
// only continue if the user pressed "OK"
163+
if (dialog.open() != Window.OK)
146164
{
147-
item.dispose();
165+
return;
148166
}
149167

168+
setSelectedProject((IProject) dialog.getFirstResult());
169+
}
170+
171+
private void createTableItem(Image image, String fileName, String filePath, String gcnoDate, String gcdaDate, String gcnoSize, String gcdaSize, Object data)
172+
{
173+
int index = 0;
174+
TableItem item = new TableItem(table, SWT.NONE);
175+
item.setImage(index, image);
176+
item.setText(index++, fileName);
177+
item.setText(index++, filePath);
178+
item.setText(index++, gcnoDate);
179+
item.setText(index++, gcdaDate);
180+
item.setText(index++, gcnoSize);
181+
item.setText(index++, gcdaSize);
182+
183+
item.setData(data);
184+
}
185+
186+
private String getFileSize(java.nio.file.Path path)
187+
{
188+
try
189+
{
190+
long size = Files.size(path);
191+
return String.valueOf(size) + " bytes"; //$NON-NLS-1$
192+
}
193+
catch (Exception e)
194+
{
195+
return Messages.Table_Unknown;
196+
}
197+
}
198+
199+
private void verifyProjectSelection()
200+
{
150201
if (getSelectedProject() == null)
151202
{
152203
// Get the current selection
@@ -173,22 +224,19 @@ private void refreshList()
173224
// If no project is selected, ask the user to choose one
174225
if (getSelectedProject() == null)
175226
{
176-
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
177-
ElementListSelectionDialog dialog = new ElementListSelectionDialog(
178-
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
179-
new org.eclipse.ui.model.WorkbenchLabelProvider());
180-
dialog.setElements(root.getProjects());
181-
dialog.setTitle(Messages.Dialog_SelectProject_Title);
182-
183-
// only continue if the user pressed "OK"
184-
if (dialog.open() != Window.OK)
185-
{
186-
return;
187-
}
188-
189-
setSelectedProject((IProject) dialog.getFirstResult());
227+
openProjectSelectionDialog();
190228
}
191229
}
230+
}
231+
232+
private void refreshList()
233+
{
234+
for (TableItem item : table.getItems())
235+
{
236+
item.dispose();
237+
}
238+
239+
verifyProjectSelection();
192240

193241
IProject project = getSelectedProject();
194242

@@ -210,47 +258,23 @@ public boolean visit(IResource resource)
210258
+ file.getName().substring(0, file.getName().indexOf(".gcno")) + ".gcda"; //$NON-NLS-1$ //$NON-NLS-2$
211259
if (Files.exists(Paths.get(partnerFile)))
212260
{
213-
TableItem item = new TableItem(table, SWT.NONE);
214261
Image image = PlatformUI.getWorkbench().getEditorRegistry()
215262
.getImageDescriptor(file.getName()).createImage();
216-
item.setImage(0, image);
217-
item.setText(0, file.getName().substring(0, file.getName().indexOf(".gcno"))); //$NON-NLS-1$
218-
item.setText(1, file.getParent().getFullPath().toString());
219-
220263
// gcno
221264
IFileInfo fileInfo = EFS.getLocalFileSystem().getStore(file.getLocationURI())
222265
.fetchInfo();
223-
String date = DateFormat.getDateTimeInstance().format(new Date(fileInfo.getLastModified()));
224-
item.setText(2, date);
225-
266+
String dateGcno = DateFormat.getDateTimeInstance()
267+
.format(new Date(fileInfo.getLastModified()));
226268
// gcda
227269
fileInfo = EFS.getLocalFileSystem().getStore(Path.fromOSString(partnerFile))
228270
.fetchInfo();
229-
item.setText(3, DateFormat.getDateTimeInstance().format(new Date(fileInfo.getLastModified())));
230-
231-
java.nio.file.Path path = Paths.get(file.getRawLocationURI());
232-
try
233-
{
234-
long size = Files.size(path);
235-
item.setText(4, String.valueOf(size) + " bytes"); //$NON-NLS-1$
236-
}
237-
catch (Exception e)
238-
{
239-
item.setText(4, Messages.Table_Unknown);
240-
}
241-
242-
path = Paths.get(partnerFile);
243-
try
244-
{
245-
long size = Files.size(path);
246-
item.setText(5, String.valueOf(size) + " bytes"); //$NON-NLS-1$
247-
}
248-
catch (Exception e)
249-
{
250-
item.setText(5, Messages.Table_Unknown);
251-
}
252-
253-
item.setData(file);
271+
String dateGcda = DateFormat.getDateTimeInstance()
272+
.format(new Date(fileInfo.getLastModified()));
273+
274+
String gcnoSize = getFileSize(Paths.get(file.getRawLocationURI()));
275+
String gcdaSize = getFileSize(Paths.get(partnerFile));
276+
277+
createTableItem(image, file.getName().substring(0, file.getName().indexOf(".gcno")), file.getParent().getFullPath().toString(), dateGcno, dateGcda, gcnoSize, gcdaSize, file);
254278
}
255279
}
256280
}
@@ -347,6 +371,18 @@ public int compare(TableItem item1, TableItem item2)
347371
});
348372

349373
// Copy existing item data
374+
List<TableRowData> copiedData = createTableDataCopy(items);
375+
376+
// Remove all items (dispose of TableItem objects)
377+
table.removeAll();
378+
379+
// Repopulate the table
380+
populateTable(copiedData);
381+
}
382+
383+
384+
private List<TableRowData> createTableDataCopy(TableItem[] items)
385+
{
350386
List<TableRowData> copiedData = new ArrayList<>();
351387
for (TableItem item : items)
352388
{
@@ -360,12 +396,13 @@ public int compare(TableItem item1, TableItem item2)
360396
rowData.itemData = item.getData();
361397
copiedData.add(rowData);
362398
}
363-
364-
// Remove all items (dispose of TableItem objects)
365-
table.removeAll();
366-
367-
// Repopulate the table
368-
for (TableRowData rowData : copiedData)
399+
400+
return copiedData;
401+
}
402+
403+
private void populateTable(List<TableRowData> tableRowData)
404+
{
405+
for (TableRowData rowData : tableRowData)
369406
{
370407
TableItem newItem = new TableItem(table, SWT.NONE);
371408
for (int j = 0; j < table.getColumnCount(); j++)

0 commit comments

Comments
 (0)