From ca4acb1be6d7b7de1008b7ec56114cf0604c1888 Mon Sep 17 00:00:00 2001 From: Saransh Saini <66969478+saranshsaini@users.noreply.github.com> Date: Fri, 24 Jan 2025 21:45:55 -0800 Subject: [PATCH] More indexing changes (#159) Indexing Fixes --- CodeiumVS/LanguageServer/LanguageServer.cs | 40 ++++++++++------------ CodeiumVS/source.extension.vsixmanifest | 2 +- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/CodeiumVS/LanguageServer/LanguageServer.cs b/CodeiumVS/LanguageServer/LanguageServer.cs index fdb5cd3..bdd2fce 100644 --- a/CodeiumVS/LanguageServer/LanguageServer.cs +++ b/CodeiumVS/LanguageServer/LanguageServer.cs @@ -759,6 +759,7 @@ private async Task InitializeTrackedWorkspaceAsync() string projectListPath = _package.SettingsPage.IndexingFilesListPath.Trim(); try { + projectListPath = projectListPath.Trim(); if (!string.IsNullOrEmpty(projectListPath) && File.Exists(projectListPath)) { string[] lines = File.ReadAllLines(projectListPath); @@ -782,10 +783,11 @@ private async Task InitializeTrackedWorkspaceAsync() } List projectsToIndex = new List(inputFilesToIndex); - projectsToIndex.AddRange(await GetFilesToIndex(openFilePaths, dte)); + int maxToIndex = 10; + projectsToIndex.AddRange(await GetFilesToIndex(inputFilesToIndex, openFilePaths, maxToIndex - projectsToIndex.Count, dte)); await _package.LogAsync($"Number of projects to index: {projectsToIndex.Count}"); - for (int i = 0; i < projectsToIndex.Count; i++) + for (int i = 0; i < Math.Min(maxToIndex, projectsToIndex.Count); i++) { try { @@ -803,25 +805,26 @@ private async Task InitializeTrackedWorkspaceAsync() } } - private async Task> GetFilesToIndex(HashSet openFilePaths, DTE dte) + private async Task> GetFilesToIndex(HashSet processedProjects, HashSet openFilePaths, int remainingToFind, DTE dte) { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); - int maxToIndex = 15; HashSet openFilesProjectsToIndexPath = new HashSet(); HashSet remainingProjectsToIndexPath = new HashSet(); - HashSet processedProjects = new HashSet(); + // Safeguard against any edge case + int maxRecursiveCalls = 25; async Task AddFilesToIndexLists(EnvDTE.Project project) { - if (openFilePaths.Count == 0 && (openFilesProjectsToIndexPath.Count + remainingProjectsToIndexPath.Count) >= maxToIndex) + maxRecursiveCalls--; + if (remainingToFind <= 0 || (openFilePaths.Count == 0 && remainingProjectsToIndexPath.Count >= remainingToFind) || maxRecursiveCalls == 0) { return; } string projectFullName = project.FullName; string projectName = Path.GetFileNameWithoutExtension(projectFullName); - if (!string.IsNullOrEmpty(projectFullName) && !processedProjects.Contains(projectFullName)) + if (!string.IsNullOrEmpty(projectFullName) && !processedProjects.Any(p => projectFullName.StartsWith(p))) { string projectDir = Path.GetDirectoryName(projectFullName); - HashSet sourceDirectories = new HashSet { projectDir }; + string projectCommonRoot = projectDir; // Parse the csproj file to find all source directories if (File.Exists(projectFullName) && (projectFullName.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase) || projectFullName.EndsWith(".vcxproj", StringComparison.OrdinalIgnoreCase))) @@ -861,16 +864,16 @@ async Task AddFilesToIndexLists(EnvDTE.Project project) foreach (var path in fullPaths.Skip(1)) { string directory = Path.GetDirectoryName(path); - while (!directory.StartsWith(commonRoot, StringComparison.OrdinalIgnoreCase) && commonRoot.Length > 3) + while (!directory.StartsWith(commonRoot, StringComparison.OrdinalIgnoreCase) && commonRoot.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).Where(s => !string.IsNullOrWhiteSpace(s)).Count() > 4) { commonRoot = Path.GetDirectoryName(commonRoot); } } - await _package.LogAsync($"Common root directory: {commonRoot}"); if (Directory.Exists(commonRoot)) { - sourceDirectories.Add(commonRoot); + await _package.LogAsync($"Common root directory: {commonRoot}"); + projectCommonRoot = commonRoot; } } } @@ -885,7 +888,7 @@ async Task AddFilesToIndexLists(EnvDTE.Project project) List matchingFiles = new List(); foreach (var filePath in openFilePaths) { - if (sourceDirectories.Any(dir => filePath.StartsWith(dir, StringComparison.OrdinalIgnoreCase))) + if (filePath.StartsWith(projectCommonRoot, StringComparison.OrdinalIgnoreCase)) { await _package.LogAsync($"Found in open files {filePath}"); matchingFiles.Add(filePath); @@ -893,10 +896,8 @@ async Task AddFilesToIndexLists(EnvDTE.Project project) } if (matchingFiles.Count > 0) { - foreach (var dir in sourceDirectories) - { - openFilesProjectsToIndexPath.Add(dir); - } + openFilesProjectsToIndexPath.Add(projectCommonRoot); + remainingToFind--; foreach (var file in matchingFiles) { openFilePaths.Remove(file); @@ -905,11 +906,8 @@ async Task AddFilesToIndexLists(EnvDTE.Project project) } else { - await _package.LogAsync($"Found in remaining {projectName}"); - foreach (var dir in sourceDirectories) - { - remainingProjectsToIndexPath.Add(dir); - } + await _package.LogAsync($"Found in remaining {projectCommonRoot}"); + remainingProjectsToIndexPath.Add(projectCommonRoot); } processedProjects.Add(projectFullName); } diff --git a/CodeiumVS/source.extension.vsixmanifest b/CodeiumVS/source.extension.vsixmanifest index 68b1eb0..d36d913 100644 --- a/CodeiumVS/source.extension.vsixmanifest +++ b/CodeiumVS/source.extension.vsixmanifest @@ -1,7 +1,7 @@ - + Codeium The modern coding superpower: free AI code acceleration plugin for your favorite languages. Type less. Code more. Ship faster. https://www.codeium.com