Add search files popup
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
using OneOf;
|
||||
using SharpIDE.Application.Features.SolutionDiscovery;
|
||||
|
||||
namespace SharpIDE.Application.Features.Search;
|
||||
|
||||
public class FindFilesSearchResult
|
||||
{
|
||||
public required SharpIdeFile File { get; set; }
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace SharpIDE.Application.Features.Search;
|
||||
|
||||
public class SearchResult
|
||||
public class FindInFilesSearchResult
|
||||
{
|
||||
public required SharpIdeFile File { get; set; }
|
||||
public required int Line { get; set; }
|
||||
@@ -7,16 +7,16 @@ namespace SharpIDE.Application.Features.Search;
|
||||
|
||||
public static class SearchService
|
||||
{
|
||||
public static async Task<List<SearchResult>> FindInFiles(SharpIdeSolutionModel solutionModel, string searchTerm, CancellationToken cancellationToken)
|
||||
public static async Task<List<FindInFilesSearchResult>> FindInFiles(SharpIdeSolutionModel solutionModel, string searchTerm, CancellationToken cancellationToken)
|
||||
{
|
||||
if (searchTerm.Length < 4)
|
||||
if (searchTerm.Length < 4) // TODO: halt search once 100 results are found, and remove this restriction
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var timer = Stopwatch.StartNew();
|
||||
var files = solutionModel.AllFiles;
|
||||
ConcurrentBag<SearchResult> results = [];
|
||||
ConcurrentBag<FindInFilesSearchResult> results = [];
|
||||
await Parallel.ForEachAsync(files, cancellationToken, async (file, ct) =>
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested) return;
|
||||
@@ -25,7 +25,7 @@ public static class SearchService
|
||||
if (cancellationToken.IsCancellationRequested) return;
|
||||
if (line.Contains(searchTerm, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
results.Add(new SearchResult
|
||||
results.Add(new FindInFilesSearchResult
|
||||
{
|
||||
File = file,
|
||||
Line = index + 1,
|
||||
@@ -40,4 +40,31 @@ public static class SearchService
|
||||
Console.WriteLine($"Search completed in {timer.ElapsedMilliseconds} ms. Found {results.Count} results. {(cancellationToken.IsCancellationRequested ? "(Cancelled)" : "")}");
|
||||
return results.ToList();
|
||||
}
|
||||
|
||||
public static async Task<List<FindFilesSearchResult>> FindFiles(SharpIdeSolutionModel solutionModel, string searchTerm, CancellationToken cancellationToken)
|
||||
{
|
||||
if (searchTerm.Length < 2) // TODO: halt search once 100 results are found, and remove this restriction
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var timer = Stopwatch.StartNew();
|
||||
var files = solutionModel.AllFiles;
|
||||
ConcurrentBag<FindFilesSearchResult> results = [];
|
||||
await Parallel.ForEachAsync(files, cancellationToken, async (file, ct) =>
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested) return;
|
||||
if (file.Name.Contains(searchTerm, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
results.Add(new FindFilesSearchResult
|
||||
{
|
||||
File = file
|
||||
});
|
||||
}
|
||||
}
|
||||
).ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
|
||||
timer.Stop();
|
||||
Console.WriteLine($"File search completed in {timer.ElapsedMilliseconds} ms. Found {results.Count} results. {(cancellationToken.IsCancellationRequested ? "(Cancelled)" : "")}");
|
||||
return results.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
using Godot;
|
||||
using SharpIDE.Application.Features.Analysis;
|
||||
using SharpIDE.Application.Features.Search;
|
||||
|
||||
namespace SharpIDE.Godot.Features.Search.SearchAllFiles;
|
||||
|
||||
public partial class SearchAllFilesResultComponent : MarginContainer
|
||||
{
|
||||
private TextureRect _textureRect = null!;
|
||||
private Label _fileNameLabel = null!;
|
||||
private Label _filePathLabel = null!;
|
||||
private Button _button = null!;
|
||||
|
||||
private Texture2D _csharpFileIcon = ResourceLoader.Load<Texture2D>("uid://do0edciarrnp0");
|
||||
private Texture2D _folderIcon = ResourceLoader.Load<Texture2D>("uid://xc8srvqwlwng");
|
||||
|
||||
public SearchAllFilesWindow ParentSearchAllFilesWindow { get; set; } = null!;
|
||||
public FindFilesSearchResult Result { get; set; } = null!;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_button = GetNode<Button>("Button");
|
||||
_textureRect = GetNode<TextureRect>("%IconTextureRect");
|
||||
_fileNameLabel = GetNode<Label>("%FileNameLabel");
|
||||
_filePathLabel = GetNode<Label>("%FilePathLabel");
|
||||
SetValue(Result);
|
||||
_button.Pressed += OnButtonPressed;
|
||||
}
|
||||
|
||||
private void OnButtonPressed()
|
||||
{
|
||||
GodotGlobalEvents.Instance.FileExternallySelected.InvokeParallelFireAndForget(Result.File, null);
|
||||
ParentSearchAllFilesWindow.Hide();
|
||||
}
|
||||
|
||||
private void SetValue(FindFilesSearchResult result)
|
||||
{
|
||||
if (result is null) return;
|
||||
_textureRect.Texture = _csharpFileIcon;
|
||||
_fileNameLabel.Text = result.File.Name;
|
||||
_filePathLabel.Text = result.File.Path;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://dx53kr0ffp0n8
|
||||
@@ -0,0 +1,60 @@
|
||||
[gd_scene load_steps=4 format=3 uid="uid://cactda5eiy55"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://dx53kr0ffp0n8" path="res://Features/Search/SearchAllFiles/SearchAllFilesResultComponent.cs" id="1_fmccx"]
|
||||
|
||||
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_6ov2c"]
|
||||
draw_center = false
|
||||
|
||||
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_dtmd4"]
|
||||
bg_color = Color(0.18039216, 0.2627451, 0.43137255, 1)
|
||||
corner_radius_top_left = 3
|
||||
corner_radius_top_right = 3
|
||||
corner_radius_bottom_right = 3
|
||||
corner_radius_bottom_left = 3
|
||||
|
||||
[node name="SearchAllFilesResultComponent" type="MarginContainer"]
|
||||
anchors_preset = 14
|
||||
anchor_top = 0.5
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 0.5
|
||||
offset_top = -4.0
|
||||
offset_bottom = 4.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_fmccx")
|
||||
|
||||
[node name="Button" type="Button" parent="."]
|
||||
custom_minimum_size = Vector2(0, 26)
|
||||
layout_mode = 2
|
||||
theme_override_styles/normal = SubResource("StyleBoxFlat_6ov2c")
|
||||
theme_override_styles/focus = SubResource("StyleBoxFlat_dtmd4")
|
||||
|
||||
[node name="MarginContainer" type="MarginContainer" parent="Button"]
|
||||
layout_mode = 1
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
theme_override_constants/margin_left = 5
|
||||
theme_override_constants/margin_right = 5
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="Button/MarginContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="IconTextureRect" type="TextureRect" parent="Button/MarginContainer/HBoxContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
|
||||
[node name="FileNameLabel" type="Label" parent="Button/MarginContainer/HBoxContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
text = "FileName.cs"
|
||||
|
||||
[node name="FilePathLabel" type="Label" parent="Button/MarginContainer/HBoxContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
theme_override_colors/font_color = Color(1, 1, 1, 0.7411765)
|
||||
text = "/File/Path/FileName.cs"
|
||||
text_overrun_behavior = 3
|
||||
@@ -0,0 +1,56 @@
|
||||
using Godot;
|
||||
using SharpIDE.Application.Features.Search;
|
||||
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
|
||||
|
||||
namespace SharpIDE.Godot.Features.Search.SearchAllFiles;
|
||||
|
||||
public partial class SearchAllFilesWindow : PopupPanel
|
||||
{
|
||||
private LineEdit _lineEdit = null!;
|
||||
private VBoxContainer _searchResultsContainer = null!;
|
||||
public SharpIdeSolutionModel Solution { get; set; } = null!;
|
||||
private readonly PackedScene _searchResultEntryScene = ResourceLoader.Load<PackedScene>("res://Features/Search/SearchAllFiles/SearchAllFilesResultComponent.tscn");
|
||||
|
||||
private CancellationTokenSource _cancellationTokenSource = new();
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_lineEdit = GetNode<LineEdit>("%SearchLineEdit");
|
||||
_lineEdit.Text = "";
|
||||
_searchResultsContainer = GetNode<VBoxContainer>("%SearchResultsVBoxContainer");
|
||||
_searchResultsContainer.GetChildren().ToList().ForEach(s => s.QueueFree());
|
||||
_lineEdit.TextChanged += OnTextChanged;
|
||||
AboutToPopup += OnAboutToPopup;
|
||||
}
|
||||
|
||||
private void OnAboutToPopup()
|
||||
{
|
||||
_lineEdit.SelectAll();
|
||||
Callable.From(_lineEdit.GrabFocus).CallDeferred();
|
||||
}
|
||||
|
||||
private async void OnTextChanged(string newText)
|
||||
{
|
||||
await _cancellationTokenSource.CancelAsync();
|
||||
// TODO: Investigate allocations
|
||||
_cancellationTokenSource = new CancellationTokenSource();
|
||||
var token = _cancellationTokenSource.Token;
|
||||
await Task.GodotRun(() => Search(newText, token));
|
||||
}
|
||||
|
||||
private async Task Search(string text, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await SearchService.FindFiles(Solution, text, cancellationToken);
|
||||
await this.InvokeAsync(() =>
|
||||
{
|
||||
_searchResultsContainer.GetChildren().ToList().ForEach(s => s.QueueFree());
|
||||
foreach (var searchResult in result)
|
||||
{
|
||||
var resultNode = _searchResultEntryScene.Instantiate<SearchAllFilesResultComponent>();
|
||||
resultNode.Result = searchResult;
|
||||
resultNode.ParentSearchAllFilesWindow = this;
|
||||
_searchResultsContainer.AddChild(resultNode);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://dbbjoiur46dhq
|
||||
@@ -0,0 +1,69 @@
|
||||
[gd_scene load_steps=4 format=3 uid="uid://b8kytk23cfo4x"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://dbbjoiur46dhq" path="res://Features/Search/SearchAllFiles/SearchAllFilesWindow.cs" id="1_opko0"]
|
||||
[ext_resource type="PackedScene" uid="uid://d358tex0duum8" path="res://Features/Search/SearchInFiles/SearchResultComponent.tscn" id="2_ss4mx"]
|
||||
|
||||
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_cuaw5"]
|
||||
bg_color = Color(0.1764706, 0.1764706, 0.1764706, 1)
|
||||
corner_radius_top_left = 5
|
||||
corner_radius_top_right = 5
|
||||
corner_radius_bottom_right = 5
|
||||
corner_radius_bottom_left = 5
|
||||
shadow_color = Color(0, 0, 0, 0.11764706)
|
||||
shadow_size = 4
|
||||
|
||||
[node name="SearchAllFilesWindow" type="PopupPanel"]
|
||||
oversampling_override = 1.0
|
||||
initial_position = 5
|
||||
size = Vector2i(1200, 800)
|
||||
visible = true
|
||||
theme_override_styles/panel = SubResource("StyleBoxFlat_cuaw5")
|
||||
script = ExtResource("1_opko0")
|
||||
|
||||
[node name="MarginContainer" type="MarginContainer" parent="."]
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
offset_left = 4.0
|
||||
offset_top = 4.0
|
||||
offset_right = -4.0
|
||||
offset_bottom = -4.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
theme_override_constants/margin_left = 15
|
||||
theme_override_constants/margin_top = 5
|
||||
theme_override_constants/margin_right = 15
|
||||
theme_override_constants/margin_bottom = 5
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="MarginContainer/VBoxContainer"]
|
||||
custom_minimum_size = Vector2(0, 43.615)
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Label" type="Label" parent="MarginContainer/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Find Files"
|
||||
|
||||
[node name="Label2" type="Label" parent="MarginContainer/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
theme_override_font_sizes/font_size = 14
|
||||
text = " 30 matching files"
|
||||
|
||||
[node name="SearchLineEdit" type="LineEdit" parent="MarginContainer/VBoxContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
text = "Test"
|
||||
|
||||
[node name="ScrollContainer" type="ScrollContainer" parent="MarginContainer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_vertical = 3
|
||||
|
||||
[node name="SearchResultsVBoxContainer" type="VBoxContainer" parent="MarginContainer/VBoxContainer/ScrollContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
|
||||
[node name="SearchAllFilesResultComponent" parent="MarginContainer/VBoxContainer/ScrollContainer/SearchResultsVBoxContainer" instance=ExtResource("2_ss4mx")]
|
||||
layout_mode = 2
|
||||
@@ -12,7 +12,7 @@ public partial class SearchResultComponent : MarginContainer
|
||||
private Button _button = null!;
|
||||
|
||||
public SearchWindow ParentSearchWindow { get; set; } = null!;
|
||||
public SearchResult Result { get; set; } = null!;
|
||||
public FindInFilesSearchResult Result { get; set; } = null!;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
@@ -31,7 +31,7 @@ public partial class SearchResultComponent : MarginContainer
|
||||
ParentSearchWindow.Hide();
|
||||
}
|
||||
|
||||
private void SetValue(SearchResult result)
|
||||
private void SetValue(FindInFilesSearchResult result)
|
||||
{
|
||||
if (result is null) return;
|
||||
_matchingLineLabel.Text = result.LineText;
|
||||
|
||||
@@ -44,7 +44,7 @@ layout_mode = 2
|
||||
|
||||
[node name="Label" type="Label" parent="MarginContainer/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Find in Files"
|
||||
text = "Find Text in Files"
|
||||
|
||||
[node name="Label2" type="Label" parent="MarginContainer/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
@@ -14,6 +14,7 @@ using SharpIDE.Godot.Features.CodeEditor;
|
||||
using SharpIDE.Godot.Features.CustomControls;
|
||||
using SharpIDE.Godot.Features.Run;
|
||||
using SharpIDE.Godot.Features.Search;
|
||||
using SharpIDE.Godot.Features.Search.SearchAllFiles;
|
||||
using SharpIDE.Godot.Features.SolutionExplorer;
|
||||
|
||||
namespace SharpIDE.Godot;
|
||||
@@ -27,6 +28,7 @@ public partial class IdeRoot : Control
|
||||
private Button _cleanSlnButton = null!;
|
||||
private Button _restoreSlnButton = null!;
|
||||
private SearchWindow _searchWindow = null!;
|
||||
private SearchAllFilesWindow _searchAllFilesWindow = null!;
|
||||
private CodeEditorPanel _codeEditorPanel = null!;
|
||||
private SolutionExplorerPanel _solutionExplorerPanel = null!;
|
||||
private InvertedVSplitContainer _invertedVSplitContainer = null!;
|
||||
@@ -61,6 +63,7 @@ public partial class IdeRoot : Control
|
||||
_runMenuButton = GetNode<Button>("%RunMenuButton");
|
||||
_codeEditorPanel = GetNode<CodeEditorPanel>("%CodeEditorPanel");
|
||||
_searchWindow = GetNode<SearchWindow>("%SearchWindow");
|
||||
_searchAllFilesWindow = GetNode<SearchAllFilesWindow>("%SearchAllFilesWindow");
|
||||
_solutionExplorerPanel = GetNode<SolutionExplorerPanel>("%SolutionExplorerPanel");
|
||||
_runPanel = GetNode<RunPanel>("%RunPanel");
|
||||
_invertedVSplitContainer = GetNode<InvertedVSplitContainer>("%InvertedVSplitContainer");
|
||||
@@ -122,6 +125,7 @@ public partial class IdeRoot : Control
|
||||
_codeEditorPanel.Solution = solutionModel;
|
||||
_bottomPanelManager.Solution = solutionModel;
|
||||
_searchWindow.Solution = solutionModel;
|
||||
_searchAllFilesWindow.Solution = solutionModel;
|
||||
Singletons.FileChangeHandler.SolutionModel = solutionModel;
|
||||
Callable.From(_solutionExplorerPanel.RepopulateTree).CallDeferred();
|
||||
RoslynAnalysis.StartSolutionAnalysis(solutionModel);
|
||||
@@ -154,5 +158,9 @@ public partial class IdeRoot : Control
|
||||
{
|
||||
_searchWindow.Popup();
|
||||
}
|
||||
else if (@event.IsActionPressed(InputStringNames.FindFiles))
|
||||
{
|
||||
_searchAllFilesWindow.Popup();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
[gd_scene load_steps=18 format=3 uid="uid://b2oniigcp5ew5"]
|
||||
[gd_scene load_steps=19 format=3 uid="uid://b2oniigcp5ew5"]
|
||||
|
||||
[ext_resource type="FontFile" uid="uid://38igu11xwba6" path="res://Inter-VariableFont.ttf" id="1_7ptyn"]
|
||||
[ext_resource type="Script" uid="uid://bavypuy7b375x" path="res://IdeRoot.cs" id="1_whawi"]
|
||||
@@ -14,6 +14,7 @@
|
||||
[ext_resource type="PackedScene" uid="uid://dkjips8oudqou" path="res://Features/Debug_/DebugPanel.tscn" id="11_s2dv6"]
|
||||
[ext_resource type="PackedScene" uid="uid://8lk0qj233a7p" path="res://Features/Search/SearchInFiles/SearchWindow.tscn" id="13_7ptyn"]
|
||||
[ext_resource type="PackedScene" uid="uid://b0tjuqq3bca5e" path="res://Features/IdeDiagnostics/IdeDiagnosticsPanel.tscn" id="13_woo5i"]
|
||||
[ext_resource type="PackedScene" uid="uid://b8kytk23cfo4x" path="res://Features/Search/SearchAllFiles/SearchAllFilesWindow.tscn" id="15_gh8b1"]
|
||||
|
||||
[sub_resource type="Theme" id="Theme_s2dv6"]
|
||||
default_font = ExtResource("1_7ptyn")
|
||||
@@ -182,3 +183,7 @@ layout_mode = 1
|
||||
[node name="SearchWindow" parent="." instance=ExtResource("13_7ptyn")]
|
||||
unique_name_in_owner = true
|
||||
visible = false
|
||||
|
||||
[node name="SearchAllFilesWindow" parent="." instance=ExtResource("15_gh8b1")]
|
||||
unique_name_in_owner = true
|
||||
visible = false
|
||||
|
||||
@@ -7,6 +7,7 @@ public static class InputStringNames
|
||||
public static readonly StringName CodeFixes = "CodeFixes";
|
||||
public static readonly StringName StepOver = "StepOver";
|
||||
public static readonly StringName FindInFiles = nameof(FindInFiles);
|
||||
public static readonly StringName FindFiles = nameof(FindFiles);
|
||||
public static readonly StringName SaveFile = nameof(SaveFile);
|
||||
public static readonly StringName SaveAllFiles = nameof(SaveAllFiles);
|
||||
}
|
||||
@@ -61,3 +61,8 @@ SaveAllFiles={
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":true,"ctrl_pressed":true,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
FindFiles={
|
||||
"deadzone": 0.2,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":true,"ctrl_pressed":true,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":84,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user