-
Notifications
You must be signed in to change notification settings - Fork 67
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
yonip23
wants to merge
3
commits into
master
Choose a base branch
from
DEV2-2327-goto-def
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,7 @@ test { | |
|
||
intellij { | ||
version = '2019.3' | ||
type = 'IC' | ||
type = 'IU' | ||
updateSinceUntilBuild = false | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
69
src/main/java/com/tabnine/psi/importsResolver/ImportsResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/tabnine/psi/importsResolver/JavaImportsResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/tabnine/psi/importsResolver/JavaScriptImportsResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/main/java/com/tabnine/psi/importsResolver/ResolverFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/tabnine/psi/importsResolver/TypeScriptImportsResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"/> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: rollback before merging