Skip to content

Commit df2dce1

Browse files
committed
feat: add interface describ
1 parent 4ce0797 commit df2dce1

File tree

1 file changed

+158
-2
lines changed

1 file changed

+158
-2
lines changed

jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/parser/ContextResolver.java

Lines changed: 158 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,151 @@ private static String convertHtmlEntities(String text) {
672672
}
673673

674674
/**
675-
* Generate human-readable method signature
675+
* Extract summary description from method JavaDoc
676+
* Returns the first sentence or paragraph of the JavaDoc as a brief description
677+
*/
678+
private static String extractMethodJavaDocSummary(IMethod method) {
679+
try {
680+
// Try to get JavaDoc from source
681+
org.eclipse.jdt.core.ISourceRange javadocRange = method.getJavadocRange();
682+
if (javadocRange == null) {
683+
return "";
684+
}
685+
686+
String rawJavadoc = method.getCompilationUnit().getSource()
687+
.substring(javadocRange.getOffset(), javadocRange.getOffset() + javadocRange.getLength());
688+
689+
if (!isNotEmpty(rawJavadoc)) {
690+
return "";
691+
}
692+
693+
// Clean the JavaDoc comment
694+
String cleaned = cleanJavadocComment(rawJavadoc);
695+
696+
// Extract the description (before any @param, @return, @throws tags)
697+
String description = extractJavadocDescription(cleaned);
698+
699+
// Get first sentence or limit length
700+
String summary = getFirstSentenceOrLimit(description, 120);
701+
702+
return summary;
703+
704+
} catch (Exception e) {
705+
// Silently fail and return empty string
706+
return "";
707+
}
708+
}
709+
710+
/**
711+
* Extract the main description part from JavaDoc (before @tags)
712+
*/
713+
private static String extractJavadocDescription(String cleanedJavadoc) {
714+
if (cleanedJavadoc == null || cleanedJavadoc.isEmpty()) {
715+
return "";
716+
}
717+
718+
// Split into lines and extract description before @tags
719+
String[] lines = cleanedJavadoc.split("\\n");
720+
StringBuilder description = new StringBuilder();
721+
722+
for (String line : lines) {
723+
String trimmedLine = line.trim();
724+
// Check if line starts with @tag
725+
if (trimmedLine.startsWith("@")) {
726+
break; // Stop at first tag
727+
}
728+
729+
// Skip empty lines at the beginning
730+
if (description.length() == 0 && trimmedLine.isEmpty()) {
731+
continue;
732+
}
733+
734+
if (description.length() > 0) {
735+
description.append(" ");
736+
}
737+
description.append(trimmedLine);
738+
}
739+
740+
return description.toString().trim();
741+
}
742+
743+
/**
744+
* Get the first sentence or limit the text to maxLength characters
745+
*/
746+
private static String getFirstSentenceOrLimit(String text, int maxLength) {
747+
if (text == null || text.isEmpty()) {
748+
return "";
749+
}
750+
751+
// Try to find the first sentence (ending with ., !, or ?)
752+
int firstPeriod = text.indexOf(". ");
753+
int firstExclamation = text.indexOf("! ");
754+
int firstQuestion = text.indexOf("? ");
755+
756+
int firstSentenceEnd = -1;
757+
if (firstPeriod != -1) firstSentenceEnd = firstPeriod;
758+
if (firstExclamation != -1 && (firstSentenceEnd == -1 || firstExclamation < firstSentenceEnd)) {
759+
firstSentenceEnd = firstExclamation;
760+
}
761+
if (firstQuestion != -1 && (firstSentenceEnd == -1 || firstQuestion < firstSentenceEnd)) {
762+
firstSentenceEnd = firstQuestion;
763+
}
764+
765+
// If we found a sentence ending and it's within reasonable length
766+
if (firstSentenceEnd != -1 && firstSentenceEnd < maxLength) {
767+
return text.substring(0, firstSentenceEnd + 1).trim();
768+
}
769+
770+
// Otherwise, limit to maxLength
771+
if (text.length() > maxLength) {
772+
// Try to cut at a word boundary
773+
int lastSpace = text.lastIndexOf(' ', maxLength);
774+
if (lastSpace > maxLength / 2) {
775+
return text.substring(0, lastSpace).trim() + "...";
776+
}
777+
return text.substring(0, maxLength).trim() + "...";
778+
}
779+
780+
return text.trim();
781+
}
782+
783+
/**
784+
* Extract summary description from field JavaDoc
785+
*/
786+
private static String extractFieldJavaDocSummary(org.eclipse.jdt.core.IField field) {
787+
try {
788+
// Try to get JavaDoc from source
789+
org.eclipse.jdt.core.ISourceRange javadocRange = field.getJavadocRange();
790+
if (javadocRange == null) {
791+
return "";
792+
}
793+
794+
String rawJavadoc = field.getCompilationUnit().getSource()
795+
.substring(javadocRange.getOffset(), javadocRange.getOffset() + javadocRange.getLength());
796+
797+
if (!isNotEmpty(rawJavadoc)) {
798+
return "";
799+
}
800+
801+
// Clean the JavaDoc comment
802+
String cleaned = cleanJavadocComment(rawJavadoc);
803+
804+
// Extract the description (before any @tags)
805+
String description = extractJavadocDescription(cleaned);
806+
807+
// Get first sentence or limit length
808+
String summary = getFirstSentenceOrLimit(description, 120);
809+
810+
return summary;
811+
812+
} catch (Exception e) {
813+
// Silently fail and return empty string
814+
return "";
815+
}
816+
}
817+
818+
/**
819+
* Generate human-readable method signature with JavaDoc description
676820
*/
677821
public static String generateMethodSignature(IMethod method) {
678822
StringBuilder sb = new StringBuilder();
@@ -731,11 +875,17 @@ public static String generateMethodSignature(IMethod method) {
731875
return method.getElementName() + "(...)";
732876
}
733877

878+
// Extract JavaDoc description and prepend if exists
879+
String javadocSummary = extractMethodJavaDocSummary(method);
880+
if (isNotEmpty(javadocSummary)) {
881+
return "// " + javadocSummary + "\n " + sb.toString();
882+
}
883+
734884
return sb.toString();
735885
}
736886

737887
/**
738-
* Generate human-readable field signature
888+
* Generate human-readable field signature with JavaDoc description
739889
*/
740890
public static String generateFieldSignature(org.eclipse.jdt.core.IField field) {
741891
StringBuilder sb = new StringBuilder();
@@ -766,6 +916,12 @@ public static String generateFieldSignature(org.eclipse.jdt.core.IField field) {
766916
return field.getElementName();
767917
}
768918

919+
// Extract JavaDoc description and prepend if exists
920+
String javadocSummary = extractFieldJavaDocSummary(field);
921+
if (isNotEmpty(javadocSummary)) {
922+
return "// " + javadocSummary + "\n " + sb.toString();
923+
}
924+
769925
return sb.toString();
770926
}
771927

0 commit comments

Comments
 (0)