glob search working
This commit is contained in:
parent
67888c7cfe
commit
bbb180531e
2 changed files with 194 additions and 90 deletions
|
|
@ -7,6 +7,8 @@ import com.github.continuedev.continueintellijextension.utils.*
|
|||
import com.intellij.codeInsight.daemon.impl.HighlightInfo
|
||||
import com.intellij.execution.configurations.GeneralCommandLine
|
||||
import com.intellij.execution.util.ExecUtil
|
||||
import com.intellij.find.FindModel
|
||||
import com.intellij.find.impl.FindInProjectUtil
|
||||
import com.intellij.ide.plugins.PluginManager
|
||||
import com.intellij.ide.plugins.PluginManagerCore
|
||||
import com.intellij.lang.annotation.HighlightSeverity
|
||||
|
|
@ -20,8 +22,10 @@ import com.intellij.openapi.editor.impl.DocumentMarkupModel
|
|||
import com.intellij.openapi.extensions.PluginId
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager
|
||||
import com.intellij.openapi.progress.EmptyProgressIndicator
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.project.guessProjectDir
|
||||
import com.intellij.openapi.roots.ProjectFileIndex
|
||||
import com.intellij.openapi.util.IconLoader
|
||||
import com.intellij.openapi.vfs.LocalFileSystem
|
||||
import com.intellij.openapi.vfs.VirtualFileManager
|
||||
|
|
@ -29,6 +33,8 @@ import com.intellij.psi.PsiDocumentManager
|
|||
import com.intellij.psi.search.FilenameIndex
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import com.intellij.testFramework.LightVirtualFile
|
||||
import com.intellij.usages.FindUsagesProcessPresentation
|
||||
import com.intellij.usages.UsageViewPresentation
|
||||
import kotlinx.coroutines.*
|
||||
import java.awt.Toolkit
|
||||
import java.awt.datatransfer.DataFlavor
|
||||
|
|
@ -327,58 +333,67 @@ class IntelliJIDE(
|
|||
return listOf("")
|
||||
}
|
||||
override suspend fun getSearchResults(query: String): String {
|
||||
// val findModel = FindModel().apply {
|
||||
// stringToFind = searchText
|
||||
// isRegularExpressions = false
|
||||
// isWholeWordsOnly = false
|
||||
// isCaseSensitive = false
|
||||
// searchContext = FindModel.SearchContext.ANY
|
||||
// isGlobal = true
|
||||
// }
|
||||
val ideInfo = this.getIdeInfo()
|
||||
if (ideInfo.remoteName != "local") {
|
||||
val command = GeneralCommandLine(
|
||||
ripgrep,
|
||||
"-i",
|
||||
"--ignore-file",
|
||||
".continueignore",
|
||||
"--ignore-file",
|
||||
".gitignore",
|
||||
"-C",
|
||||
"2",
|
||||
"--heading",
|
||||
"-e",
|
||||
query,
|
||||
"."
|
||||
)
|
||||
|
||||
// val findManager = FindManager.getInstance(project)
|
||||
// val scope = GlobalSearchScope.projectScope(project)
|
||||
|
||||
// FindInProjectUtil.findUsages(
|
||||
// findModel,
|
||||
// project.basePath,
|
||||
// project,
|
||||
// {
|
||||
// // Process each found usage
|
||||
// val virtualFile = it.virtualFile
|
||||
// val lineNumber = it.line
|
||||
// // Handle result
|
||||
// },
|
||||
// scope
|
||||
// )
|
||||
|
||||
// return withContext(Dispatchers.IO) {
|
||||
// val findModel = FindModel().apply {
|
||||
// stringToFind = query
|
||||
// isRegularExpressions = false
|
||||
// isWholeWordsOnly = false
|
||||
// isCaseSensitive = false
|
||||
// searchContext = UsageSearchContext.ANY
|
||||
// }
|
||||
|
||||
// val results = StringBuilder()
|
||||
// runReadAction {
|
||||
// FindInProjectUtil.findUsages(
|
||||
// findModel,
|
||||
// project.baseDir,
|
||||
// {
|
||||
// results.append("${it.file.path}:${it.line + 1}: ${it.lineText}\n")
|
||||
// true
|
||||
// },
|
||||
// null
|
||||
// )
|
||||
// }
|
||||
// results.toString()
|
||||
// }
|
||||
val command = GeneralCommandLine(ripgrep, "-i", "-C", "2", "--heading", "-e", query, ".")
|
||||
command.setWorkDirectory(project.basePath)
|
||||
return ExecUtil.execAndGetOutput(command).stdout
|
||||
command.setWorkDirectory(project.basePath)
|
||||
return ExecUtil.execAndGetOutput(command).stdout
|
||||
} else {
|
||||
return "Ripgrep not supported, this workspace is remote"
|
||||
// For remote workspaces, use JetBrains search functionality
|
||||
// val searchResults = StringBuilder()
|
||||
// ApplicationManager.getApplication().invokeAndWait {
|
||||
// val options = FindModel().apply {
|
||||
// stringToFind = query
|
||||
// isCaseSensitive = false
|
||||
// isRegularExpressions = false
|
||||
// isWholeWordsOnly = false
|
||||
// searchContext = FindModel.SearchContext.ANY // or IN_CODE, IN_COMMENTS, IN_STRING_LITERALS, etc.
|
||||
// isMultiline = true // Allow matching across multiple lines
|
||||
// }
|
||||
//
|
||||
// val progressIndicator = EmptyProgressIndicator()
|
||||
// val presentation = FindUsagesProcessPresentation(
|
||||
// UsageViewPresentation()
|
||||
// )
|
||||
// val filesToSearch = ProjectFileIndex.getInstance(project)
|
||||
// .iterateContent(::ArrayList)
|
||||
// .filterNot { it.isDirectory }
|
||||
// .toSet()
|
||||
//
|
||||
//
|
||||
// FindInProjectUtil.findUsages(
|
||||
// options,
|
||||
// project,
|
||||
// progressIndicator,
|
||||
// presentation,
|
||||
// filesToSearch
|
||||
// ) { result ->
|
||||
// val virtualFile = result.virtualFile
|
||||
// searchResults.append(virtualFile.path).append("\n")
|
||||
// searchResults.append("${result..trim()}\n")
|
||||
// true // continue searching
|
||||
// }
|
||||
// }
|
||||
// return searchResults.toString()
|
||||
//
|
||||
// val scope = GlobalSearchScope.projectScope(project)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun subprocess(command: String, cwd: String?): List<Any> {
|
||||
val commandList = command.split(" ")
|
||||
|
|
|
|||
|
|
@ -555,54 +555,143 @@ class VsCodeIde implements IDE {
|
|||
.map((t) => (t.input as vscode.TabInputText).uri.toString());
|
||||
}
|
||||
|
||||
runRipgrepQuery(dirUri: string, args: string[]) {
|
||||
const relativeDir = vscode.Uri.parse(dirUri).fsPath;
|
||||
const ripGrepUri = vscode.Uri.joinPath(
|
||||
getExtensionUri(),
|
||||
"out/node_modules/@vscode/ripgrep/bin/rg",
|
||||
);
|
||||
const p = child_process.spawn(ripGrepUri.fsPath, args, {
|
||||
cwd: relativeDir,
|
||||
});
|
||||
let output = "";
|
||||
|
||||
p.stdout.on("data", (data) => {
|
||||
output += data.toString();
|
||||
});
|
||||
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
p.on("error", reject);
|
||||
p.on("close", (code) => {
|
||||
if (code === 0) {
|
||||
resolve(output);
|
||||
} else if (code === 1) {
|
||||
// No matches
|
||||
resolve("No matches found");
|
||||
} else {
|
||||
reject(new Error(`Process exited with code ${code}`));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async getFileResults(pattern: string): Promise<string[]> {
|
||||
// vscode.workspace.findFiles("");
|
||||
return ["file1", "file2"];
|
||||
const MAX_FILE_RESULTS = 200;
|
||||
if (vscode.env.remoteName) {
|
||||
// TODO better tests for this remote search implementation
|
||||
// throw new Error("Ripgrep not supported, this workspace is remote");
|
||||
|
||||
// IMPORTANT: findFiles automatically accounts for .gitignore
|
||||
const ignoreFiles = await vscode.workspace.findFiles(
|
||||
"**/.continueignore",
|
||||
null,
|
||||
);
|
||||
|
||||
const ignoreGlobs: Set<string> = new Set();
|
||||
for (const file of ignoreFiles) {
|
||||
const content = await vscode.workspace.fs.readFile(file);
|
||||
const filePath = vscode.workspace.asRelativePath(file);
|
||||
const fileDir = filePath
|
||||
.replace(/\\/g, "/")
|
||||
.replace(/\/$/, "")
|
||||
.split("/")
|
||||
.slice(0, -1)
|
||||
.join("/");
|
||||
|
||||
const patterns = Buffer.from(content)
|
||||
.toString()
|
||||
.split("\n")
|
||||
.map((line) => line.trim())
|
||||
.filter(
|
||||
(line) => line && !line.startsWith("#") && !pattern.startsWith("!"),
|
||||
);
|
||||
// VSCode does not support negations
|
||||
|
||||
patterns
|
||||
// Handle prefix
|
||||
.map((pattern) => {
|
||||
const normalizedPattern = pattern.replace(/\\/g, "/");
|
||||
|
||||
if (normalizedPattern.startsWith("/")) {
|
||||
if (fileDir) {
|
||||
return `{/,}${normalizedPattern}`;
|
||||
} else {
|
||||
return `${fileDir}/${normalizedPattern.substring(1)}`;
|
||||
}
|
||||
} else {
|
||||
if (fileDir) {
|
||||
return `${fileDir}/${normalizedPattern}`;
|
||||
} else {
|
||||
return `**/${normalizedPattern}`;
|
||||
}
|
||||
}
|
||||
})
|
||||
// Handle suffix
|
||||
.map((pattern) => {
|
||||
return pattern.endsWith("/") ? `${pattern}**/*` : pattern;
|
||||
})
|
||||
.forEach((pattern) => {
|
||||
ignoreGlobs.add(pattern);
|
||||
});
|
||||
}
|
||||
|
||||
const ignoreGlobsArray = Array.from(ignoreGlobs);
|
||||
|
||||
const results = await vscode.workspace.findFiles(
|
||||
pattern,
|
||||
ignoreGlobs.size ? `{${ignoreGlobsArray.join(",")}}` : null,
|
||||
MAX_FILE_RESULTS,
|
||||
);
|
||||
return results.map((result) => vscode.workspace.asRelativePath(result));
|
||||
} else {
|
||||
const results: string[] = [];
|
||||
for (const dir of await this.getWorkspaceDirs()) {
|
||||
const dirResults = await this.runRipgrepQuery(dir, [
|
||||
"--files",
|
||||
"--iglob",
|
||||
pattern,
|
||||
"--ignore-file",
|
||||
".continueignore",
|
||||
"--ignore-file",
|
||||
".gitignore",
|
||||
]);
|
||||
|
||||
results.push(dirResults);
|
||||
}
|
||||
|
||||
return results.join("\n").split("\n").slice(0, MAX_FILE_RESULTS);
|
||||
}
|
||||
}
|
||||
|
||||
async getSearchResults(query: string): Promise<string> {
|
||||
if (vscode.env.remoteName) {
|
||||
return "Ripgrep not supported, this workspace is remote";
|
||||
throw new Error("Ripgrep not supported, this workspace is remote");
|
||||
}
|
||||
const results: string[] = [];
|
||||
for (const dir of await this.getWorkspaceDirs()) {
|
||||
const relativeDir = vscode.Uri.parse(dir).fsPath;
|
||||
const ripGrepUri = vscode.Uri.joinPath(
|
||||
getExtensionUri(),
|
||||
"out/node_modules/@vscode/ripgrep/bin/rg",
|
||||
);
|
||||
const p = child_process.spawn(
|
||||
ripGrepUri.fsPath,
|
||||
[
|
||||
"-i", // Case-insensitive search
|
||||
"-C",
|
||||
"2", // Show 2 lines of context
|
||||
"--heading", // Only show filepath once per result
|
||||
"-e",
|
||||
query, // Pattern to search for
|
||||
".", // Directory to search in
|
||||
],
|
||||
{ cwd: relativeDir },
|
||||
);
|
||||
let output = "";
|
||||
|
||||
p.stdout.on("data", (data) => {
|
||||
output += data.toString();
|
||||
});
|
||||
|
||||
const dirResults = await new Promise<string>((resolve, reject) => {
|
||||
p.on("error", reject);
|
||||
p.on("close", (code) => {
|
||||
if (code === 0) {
|
||||
resolve(output);
|
||||
} else if (code === 1) {
|
||||
// No matches
|
||||
resolve("No matches found");
|
||||
} else {
|
||||
reject(new Error(`Process exited with code ${code}`));
|
||||
}
|
||||
});
|
||||
});
|
||||
const dirResults = await this.runRipgrepQuery(dir, [
|
||||
"-i", // Case-insensitive search
|
||||
"--ignore-file",
|
||||
".continueignore",
|
||||
"--ignore-file",
|
||||
".gitignore",
|
||||
"-C",
|
||||
"2", // Show 2 lines of context
|
||||
"--heading", // Only show filepath once per result
|
||||
"-e",
|
||||
query, // Pattern to search for
|
||||
".", // Directory to search in
|
||||
]);
|
||||
|
||||
results.push(dirResults);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue