-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
173 additions
and
155 deletions.
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package siosio | ||
|
||
import com.intellij.codeInsight.completion.* | ||
|
||
interface CentralSearcher { | ||
|
||
val dependencyText: DependencyText | ||
|
||
fun find(resultSet: CompletionResultSet) | ||
} |
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,17 @@ | ||
package siosio | ||
|
||
import org.apache.http.client.methods.* | ||
import org.apache.http.impl.client.* | ||
|
||
object Client { | ||
|
||
fun get(uri: String): String { | ||
val response = HttpClients.createDefault() | ||
.execute(HttpGet(uri)) | ||
|
||
return when (response.statusLine.statusCode) { | ||
200 -> response.entity.content.reader().readText() | ||
else -> "" | ||
} | ||
} | ||
} |
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,24 @@ | ||
package siosio | ||
|
||
import com.intellij.codeInsight.completion.* | ||
import siosio.searcher.* | ||
|
||
class DependencyText(val text: String?) { | ||
|
||
val isShort: Boolean = text?.length ?: 0 < 2 | ||
|
||
private val splitText = text?.split(":") ?: emptyList() | ||
|
||
fun getGroupId() = splitText.getOrElse(0) { "" } | ||
fun getArtifactId() = splitText.getOrElse(1) { "" } | ||
|
||
fun addCompletions(resultSet: CompletionResultSet) { | ||
when (splitText.size) { | ||
3 -> VersionSearcher(this) | ||
2 -> ArtifactSearcher(this) | ||
else -> DefaultSearcher(this) | ||
}.find(resultSet) | ||
|
||
} | ||
|
||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,32 @@ | ||
package siosio.searcher | ||
|
||
import com.intellij.codeInsight.completion.* | ||
import com.intellij.codeInsight.completion.impl.* | ||
import com.intellij.codeInsight.lookup.* | ||
import siosio.* | ||
|
||
class ArtifactSearcher(override val dependencyText: DependencyText) : CentralSearcher { | ||
|
||
override fun find(resultSet: CompletionResultSet) { | ||
val text = Client.get( | ||
"https://search.maven.org/solrsearch/select?q=g:${dependencyText.getGroupId()}" + | ||
"&rows=200&wt=json") | ||
|
||
resultSet.restartCompletionOnPrefixChange(dependencyText.text) | ||
resultSet.withRelevanceSorter( | ||
CompletionSorter.emptySorter().weigh(PreferStartMatching()) | ||
).addAllElements(ARTIFACT_PATTERN.findAll(text) | ||
.map { | ||
it.groups[1]!!.value | ||
} | ||
.distinct() | ||
.map { | ||
LookupElementBuilder.create("${dependencyText.getGroupId()}:$it") | ||
}.toList()) | ||
} | ||
|
||
|
||
companion object { | ||
private val ARTIFACT_PATTERN = Regex(",\"a\":\"([^\"]+)\"") | ||
} | ||
} |
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,31 @@ | ||
package siosio.searcher | ||
|
||
import com.intellij.codeInsight.completion.* | ||
import com.intellij.codeInsight.completion.impl.* | ||
import com.intellij.codeInsight.lookup.* | ||
import siosio.* | ||
import java.awt.SystemColor.* | ||
|
||
class DefaultSearcher(override val dependencyText: DependencyText) : CentralSearcher { | ||
override fun find(resultSet: CompletionResultSet) { | ||
val result = Client.get("https://search.maven.org/solrsearch/select?q=${dependencyText.text}&rows=100&wt=json") | ||
|
||
resultSet.withRelevanceSorter( | ||
CompletionSorter.emptySorter().weigh(PreferStartMatching()) | ||
).addAllElements(ID_PATTERN.findAll(result) | ||
.mapNotNull { | ||
it.groups[1]?.value | ||
} | ||
.map { | ||
LookupElementBuilder.create(it) | ||
} | ||
.toList() | ||
) | ||
} | ||
|
||
|
||
companion object { | ||
private val ID_PATTERN = Regex("\\{\"id\":\"([^\"]+)\"") | ||
|
||
} | ||
} |
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,39 @@ | ||
package siosio.searcher | ||
|
||
import com.intellij.codeInsight.completion.* | ||
import com.intellij.codeInsight.lookup.* | ||
import siosio.* | ||
|
||
class VersionSearcher(override val dependencyText: DependencyText) : CentralSearcher { | ||
|
||
override fun find(resultSet: CompletionResultSet) { | ||
val text = Client.get( | ||
"https://search.maven.org/solrsearch/select?q=g:${dependencyText.getGroupId()}+AND+a:${dependencyText.getArtifactId()}" + | ||
"&rows=100&core=gav&wt=json") | ||
|
||
|
||
val versions = VERSION_PATTERN.findAll(text).mapNotNull { | ||
it.groups[1]?.value | ||
}.distinct().toList() | ||
|
||
resultSet.restartCompletionOnPrefixChange(dependencyText.text) | ||
val withRelevanceSorter = resultSet.withRelevanceSorter( | ||
CompletionSorter.emptySorter().weigh(object : LookupElementWeigher("gradleDependencyWeigher") { | ||
override fun weigh(element: LookupElement): Comparable<VersionComparable> { | ||
return VersionComparable(versions.indexOf(element.lookupString)) | ||
} | ||
}) | ||
) | ||
withRelevanceSorter.addAllElements(versions.map { | ||
LookupElementBuilder.create(it) | ||
}) | ||
} | ||
|
||
companion object { | ||
private val VERSION_PATTERN = Regex("\\{\"id\":\"([^\"]+)\"") | ||
|
||
class VersionComparable(private val index: Int) : Comparable<VersionComparable> { | ||
override fun compareTo(other: VersionComparable): Int = this.index - other.index | ||
} | ||
} | ||
} |
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