Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV2-2327 goto def [[WIP]] #472

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test {

intellij {
version = '2019.3'
type = 'IC'
type = 'IU'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: rollback before merging

updateSinceUntilBuild = false
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/tabnine/inline/InlineCompletionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.tabnine.intellij.completions.CompletionUtils;
import com.tabnine.prediction.CompletionFacade;
import com.tabnine.prediction.TabNineCompletion;
import com.tabnine.psi.LocationLink;
import com.tabnine.psi.importsResolver.ImportsResolverKt;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -62,6 +64,10 @@ public void retrieveAndShowCompletion(
@Nullable TabNineCompletion lastShownSuggestion,
@NotNull String userInput,
@NotNull CompletionAdjustment completionAdjustment) {
long start = System.currentTimeMillis();
List<LocationLink> links = ImportsResolverKt.resolveImportsLinksInFile(editor);
long end = System.currentTimeMillis();
Logger.getInstance(getClass()).warn("took " + (end - start) + "ms - links: " + links);
Integer tabSize = GraphicsUtilsKt.getTabSize(editor);

ObjectUtils.doIfNotNull(lastFetchInBackgroundTask, task -> task.cancel(false));
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/tabnine/psi/LocationLink.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.tabnine.psi

import java.net.URI

data class Position(val line: Int, val column: Int)

data class Range(val start: Position, val end: Position)

data class LocationLink(val targetUri: URI, val targetRange: Range)
69 changes: 69 additions & 0 deletions src/main/java/com/tabnine/psi/importsResolver/ImportsResolver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.tabnine.psi.importsResolver

import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.tabnine.psi.LocationLink
import com.tabnine.psi.Position
import com.tabnine.psi.Range
import java.net.URI

abstract class ImportsResolver {
abstract fun potentialElementsPredicate(element: PsiElement): Boolean

fun resolveImportsLinksInFile(psiFile: PsiFile): List<LocationLink> {
return psiFile.children
?.filter { potentialElementsPredicate(it) }
?.flatMap { resolveImportReference(it) } ?: emptyList()
}
}

fun resolveImportsLinksInFile(editor: Editor): List<LocationLink> {
val psiFile = editor.project
?.let { PsiDocumentManager.getInstance(it).getPsiFile(editor.document) }
?: return emptyList()
val resolver = getImportsResolver(psiFile) ?: return emptyList()
return resolver.resolveImportsLinksInFile(psiFile)
}

private fun resolveImportReference(element: PsiElement): List<LocationLink> {
val referee = element.reference?.resolve()
?: return element.children?.flatMap { resolveImportReference(it) } ?: emptyList()

if (!(referee.isValid && referee.isWritable)) return emptyList()
val filePath = referee.containingFile?.virtualFile?.url?.let(URI::create) ?: return emptyList()
val range = referee.textRange ?: return emptyList()
val text = referee.containingFile?.text ?: return emptyList()
val linesAccumulativeLengths = linesAccumulativeLengths(text)

val startPos = toLogicalPosition(linesAccumulativeLengths, range.startOffset) ?: return emptyList()
val endPos = toLogicalPosition(linesAccumulativeLengths, range.endOffset) ?: return emptyList()

return listOf(LocationLink(filePath, Range(startPos, endPos)))
}

private fun toLogicalPosition(linesAccumulativeLengths: List<Int>, offset: Int): Position? {
val line = linesAccumulativeLengths.indexOfFirst { it >= offset }

if (line == -1) return null
if (line == 0) return Position(0, offset)

val column = offset - 1 - linesAccumulativeLengths[line - 1]
return Position(line, column)
}

private fun linesAccumulativeLengths(text: String): List<Int> {
val linesAccumulativeLengths = mutableListOf<Int>()
var index = text.indexOf('\n')

while (index != -1) {
linesAccumulativeLengths.add(index)
val nextIndex = text.indexOf('\n', index + 1)
index = nextIndex
}

linesAccumulativeLengths.add(text.length)

return linesAccumulativeLengths
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.tabnine.psi.importsResolver

import com.intellij.psi.PsiElement

class JavaImportsResolver : ImportsResolver() {
override fun potentialElementsPredicate(element: PsiElement): Boolean {
return element.text.contains("import")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.tabnine.psi.importsResolver

import com.intellij.psi.PsiElement

class JavaScriptImportsResolver : ImportsResolver() {
override fun potentialElementsPredicate(element: PsiElement): Boolean {
return element.text.contains("import")
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/tabnine/psi/importsResolver/ResolverFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.tabnine.psi.importsResolver

import com.intellij.lang.Language
import com.intellij.psi.PsiFile

fun getImportsResolver(psiFile: PsiFile): ImportsResolver? {
return when (psiFile.language) {
Language.findLanguageByID("JAVA") -> {
JavaImportsResolver()
}
Language.findLanguageByID("TypeScript") -> {
TypeScriptImportsResolver()
}
Language.findLanguageByID("ECMAScript 6") -> {
JavaScriptImportsResolver()
}
else -> null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.tabnine.psi.importsResolver

import com.intellij.psi.PsiElement

class TypeScriptImportsResolver : ImportsResolver() {
override fun potentialElementsPredicate(element: PsiElement): Boolean {
return element.text.contains("import")
}
}
4 changes: 4 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@
<idea-version since-build="193"/>
<depends>com.intellij.modules.lang</depends>

<!-- <projectListeners>-->
<!-- <listener topic="com.intellij.openapi.fileEditor.FileEditorManagerListener"-->
<!-- class="com.tabnine.general.FilesListener"/>-->
<!-- </projectListeners>-->
Comment on lines +112 to +115
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: remove

<extensions defaultExtensionNs="com.intellij">
<preloadingActivity implementation="com.tabnine.Initializer"/>
<preloadingActivity implementation="com.tabnine.general.GettingStartedManager"/>
Expand Down