Implemented AUTO IMPORT SUGGESTION COMPLETIONS

This commit is contained in:
bigguy345 committed 2026-01-04 03:59:51 +02:00
1 parent 04a0fbc84d
commit ac92210771
5 files changed
+271 -9

No files matched your search

@@ -45,12 +45,17 @@ public class AutocompleteItem implements Comparable<AutocompleteItem> {
private final Object sourceData; // Original source (MethodInfo, FieldInfo, etc.)
private final boolean deprecated; // Whether this item is deprecated
// Import tracking
private final boolean requiresImport; // Whether selecting this item requires adding an import
private final String importPath; // Full path for import (e.g., "net.minecraft.client.Minecraft")
// Match scoring
private int matchScore = 0; // How well this matches the query
private int[] matchIndices; // Indices of matched characters for highlighting
private AutocompleteItem(String name, String insertText, Kind kind, String typeLabel,
String signature, String documentation, Object sourceData, boolean deprecated) {
String signature, String documentation, Object sourceData, boolean deprecated,
boolean requiresImport, String importPath) {
this.name = name;
this.insertText = insertText;
this.kind = kind;
@@ -59,6 +64,8 @@ public class AutocompleteItem implements Comparable<AutocompleteItem> {
this.documentation = documentation;
this.sourceData = sourceData;
this.deprecated = deprecated;
this.requiresImport = requiresImport;
this.importPath = importPath;
}
// ==================== FACTORY METHODS ====================
@@ -105,7 +112,9 @@ public class AutocompleteItem implements Comparable<AutocompleteItem> {
signature,
method.getDocumentation(),
method,
false // TODO: Check for @Deprecated annotation
false, // TODO: Check for @Deprecated annotation
false, // Methods don't require imports
null
);
}
@@ -139,7 +148,9 @@ public class AutocompleteItem implements Comparable<AutocompleteItem> {
typeLabel + " " + field.getName(),
null,
field,
false
false,
false, // Fields don't require imports
null
);
}
@@ -167,7 +178,9 @@ public class AutocompleteItem implements Comparable<AutocompleteItem> {
type.getFullName(),
null,
type,
false
false,
false, // Will be overridden for unimported types
null
);
}
@@ -192,7 +205,9 @@ public class AutocompleteItem implements Comparable<AutocompleteItem> {
method.getSignature(),
method.getDocumentation(),
method,
false
false,
false,
null
);
}
@@ -208,7 +223,9 @@ public class AutocompleteItem implements Comparable<AutocompleteItem> {
field.toString(),
field.getDocumentation(),
field,
false
false,
false,
null
);
}
@@ -224,7 +241,9 @@ public class AutocompleteItem implements Comparable<AutocompleteItem> {
null,
null,
null,
false
false,
false,
null
);
}
@@ -339,6 +358,8 @@ public class AutocompleteItem implements Comparable<AutocompleteItem> {
public String getDocumentation() { return documentation; }
public Object getSourceData() { return sourceData; }
public boolean isDeprecated() { return deprecated; }
public boolean requiresImport() { return requiresImport; }
public String getImportPath() { return importPath; }
public int getMatchScore() { return matchScore; }
public int[] getMatchIndices() { return matchIndices; }
@@ -409,6 +430,8 @@ public class AutocompleteItem implements Comparable<AutocompleteItem> {
private String documentation;
private Object sourceData;
private boolean deprecated = false;
private boolean requiresImport = false;
private String importPath = null;
public Builder name(String name) {
this.name = name;
@@ -450,12 +473,22 @@ public class AutocompleteItem implements Comparable<AutocompleteItem> {
return this;
}
public Builder requiresImport(boolean requiresImport) {
this.requiresImport = requiresImport;
return this;
}
public Builder importPath(String importPath) {
this.importPath = importPath;
return this;
}
public AutocompleteItem build() {
if (insertText == null) {
insertText = name;
}
return new AutocompleteItem(name, insertText, kind, typeLabel,
signature, documentation, sourceData, deprecated);
signature, documentation, sourceData, deprecated, requiresImport, importPath);
}
}
}
@@ -75,6 +75,12 @@ public class AutocompleteManager {
*/
void replaceTextRange(String text, int startPosition, int endPosition);
/**
* Add an import statement and sort all imports.
* @param importPath Full path of the class to import (e.g., "net.minecraft.client.Minecraft")
*/
void addImport(String importPath);
/**
* Get current cursor position.
*/
@@ -510,9 +516,15 @@ public class AutocompleteManager {
}
}
// Replace from prefixStart to endPos using the new method
// IMPORTANT: Do the text replacement FIRST, then add import
// This prevents position corruption since import adds text at the TOP
insertCallback.replaceTextRange(insertText, prefixStartPosition, endPos);
// If this item requires an import, add it AFTER the text replacement
if (item.requiresImport() && item.getImportPath() != null) {
insertCallback.addImport(item.getImportPath());
}
active = false;
}
@@ -224,10 +224,73 @@ public class JavaAutocompleteProvider implements AutocompleteProvider {
items.add(AutocompleteItem.fromType(scriptType));
}
// Add unimported classes that match the prefix (for auto-import)
if (context.prefix != null && context.prefix.length() >= 2 && Character.isUpperCase(context.prefix.charAt(0))) {
addUnimportedClassSuggestions(context.prefix, items);
}
// Add keywords
addKeywords(items);
}
/**
* Add suggestions for unimported classes that match the prefix.
* These will trigger auto-import when selected.
*/
private void addUnimportedClassSuggestions(String prefix, List<AutocompleteItem> items) {
// Get the type resolver
TypeResolver resolver = TypeResolver.getInstance();
// Find classes matching this prefix (not just exact matches)
List<String> matchingClasses = resolver.findClassesByPrefix(prefix, 15);
// Track what's already imported to avoid duplicates
Set<String> importedFullNames = new HashSet<>();
for (TypeInfo imported : document.getImportedTypes()) {
importedFullNames.add(imported.getFullName());
}
// Also track simple names already in the list to avoid showing both
// imported and unimported versions of the same class
Set<String> existingSimpleNames = new HashSet<>();
for (AutocompleteItem item : items) {
if (item.getKind() == AutocompleteItem.Kind.CLASS ||
item.getKind() == AutocompleteItem.Kind.ENUM) {
existingSimpleNames.add(item.getName());
}
}
for (String fullName : matchingClasses) {
// Skip if already imported
if (importedFullNames.contains(fullName)) {
continue;
}
TypeInfo type = resolver.resolveFullName(fullName);
if (type != null && type.isResolved()) {
// Skip if a class with this simple name is already in the list
if (existingSimpleNames.contains(type.getSimpleName())) {
continue;
}
// Create an item that requires import
AutocompleteItem item = new AutocompleteItem.Builder()
.name(type.getSimpleName())
.insertText(type.getSimpleName())
.kind(type.getKind() == TypeInfo.Kind.ENUM ?
AutocompleteItem.Kind.ENUM : AutocompleteItem.Kind.CLASS)
.typeLabel(type.getPackageName())
.signature(type.getFullName())
.sourceData(type)
.requiresImport(true)
.importPath(fullName)
.build();
items.add(item);
existingSimpleNames.add(type.getSimpleName());
}
}
}
/**
* Add Java keywords.
*/
@@ -391,6 +391,29 @@ public class TypeResolver {
word.equals("native") || word.equals("strictfp");
}
// ==================== CLASS SEARCH ====================
/**
* Search for classes matching the given prefix.
* Uses ClassIndex for O(n) lookup where n is the number of indexed classes.
*
* @param prefix The prefix to match (case-sensitive for class names)
* @param maxResults Maximum number of results to return
* @return List of fully-qualified class names that match
*/
public List<String> findClassesByPrefix(String prefix, int maxResults) {
return ClassIndex.getInstance().findByPrefix(prefix, maxResults);
}
/**
* @deprecated Use findClassesByPrefix instead for prefix matching
*/
@Deprecated
public List<String> findClassesBySimpleName(String simpleName, int maxResults) {
// Redirect to prefix matching for backwards compatibility
return findClassesByPrefix(simpleName, maxResults);
}
/**
* Represents a type occurrence within generic content.
*/