Skip to content

Commit

Permalink
検索方法を見直し
Browse files Browse the repository at this point in the history
  • Loading branch information
siosio committed Jan 5, 2018
1 parent 546502b commit b0ba003
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 155 deletions.
10 changes: 10 additions & 0 deletions src/main/java/siosio/CentralSearcher.kt
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)
}
17 changes: 17 additions & 0 deletions src/main/java/siosio/Client.kt
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 -> ""
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/siosio/DependencyText.kt
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)

}

}
78 changes: 17 additions & 61 deletions src/main/java/siosio/GradleDependenciesCompletionContributor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,72 +14,28 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.*

class GradleDependenciesCompletionContributor : AbstractGradleCompletionContributor() {

init {
extend(CompletionType.SMART,
psiElement(PsiElement::class.java)
.and(psiElement().inFile(PlatformPatterns.psiFile().withName(StandardPatterns.string().endsWith(".gradle"))))
.withParent(GrLiteral::class.java)
.withSuperParent(5, psiElement(GrMethodCallExpression::class.java)
.withText(string().contains("dependencies"))), CompletionParametersCompletionProvider())
}

private class CompletionParametersCompletionProvider : CompletionProvider<CompletionParameters>() {

val mavenFinder = MavenFinder()

override fun addCompletions(
parameters: CompletionParameters,
context: ProcessingContext,
resultSet: CompletionResultSet) {

val position = parameters.originalPosition
init {
extend(CompletionType.SMART,
psiElement(PsiElement::class.java)
.and(psiElement().inFile(PlatformPatterns.psiFile().withName(StandardPatterns.string().endsWith(".gradle"))))
.withParent(GrLiteral::class.java)
.withSuperParent(5, psiElement(GrMethodCallExpression::class.java)
.withText(string().contains("dependencies"))), CompletionParametersCompletionProvider())
}

position?.let {
val text = trimQuote(it.text)
if (isShortText(text)) {
return
}
private class CompletionParametersCompletionProvider : CompletionProvider<CompletionParameters>() {

val searchParam = SearchParam(text)
override fun addCompletions(
parameters: CompletionParameters,
context: ProcessingContext,
resultSet: CompletionResultSet) {

val searchResult = mavenFinder.find(searchParam)
val completionResultSet = if (searchParam.isFindVersion()) {
resultSet.restartCompletionOnPrefixChange(text)
resultSet.withRelevanceSorter(
CompletionSorter.emptySorter().weigh(object : LookupElementWeigher("gradleDependencyWeigher") {
override fun weigh(element: LookupElement): Comparable<*> {
return DependencyComparable(searchResult, element)
}
})
)
} else {
resultSet.withRelevanceSorter(
CompletionSorter.emptySorter().weigh(PreferStartMatching())
)
}
val position = parameters.originalPosition ?: return

searchResult.forEach {
completionResultSet.addElement(LookupElementBuilder.create(it))
DependencyText(trimQuote(position.text))
.takeUnless { it.isShort }
?.addCompletions(resultSet)
}
}
}
}

class DependencyComparable(
versions: Set<String>,
private val element: LookupElement) : Comparable<DependencyComparable> {

private val index: Int;

init {
index = versions.indexOf(element.lookupString)
}

override fun compareTo(other: DependencyComparable): Int = this.index - other.index
}

companion object {
fun isShortText(text: String?) = (text?.length ?: 0) < 2
}
}

66 changes: 0 additions & 66 deletions src/main/java/siosio/MavenFinder.kt

This file was deleted.

23 changes: 0 additions & 23 deletions src/main/java/siosio/SearchParam.kt

This file was deleted.

32 changes: 32 additions & 0 deletions src/main/java/siosio/searcher/ArtifactSearcher.kt
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\":\"([^\"]+)\"")
}
}
31 changes: 31 additions & 0 deletions src/main/java/siosio/searcher/DefaultSearcher.kt
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\":\"([^\"]+)\"")

}
}
39 changes: 39 additions & 0 deletions src/main/java/siosio/searcher/VersionSearcher.kt
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
}
}
}
8 changes: 3 additions & 5 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin>
<id>GradleDependenciesHelper</id>
<name>Gradle Dependencies Helper</name>
<version>1.9</version>
<version>1.10</version>
<vendor email="siosio12@gmail.com" url="https://github.com/siosio/GradleDependenciesHelperPlugin">siosio</vendor>
<depends>org.jetbrains.plugins.gradle</depends>
<depends>org.intellij.groovy</depends>
Expand Down Expand Up @@ -34,12 +34,10 @@
]]>
</change-notes>

<idea-version since-build="130.1619"/>
<idea-version since-build="173.0"/>

<extensions defaultExtensionNs="com.intellij">
<completion.contributor language="Groovy"
implementationClass="siosio.GradleDependenciesCompletionContributor" order="first" />

<completion.contributor language="Groovy" implementationClass="siosio.GradleDependenciesCompletionContributor" order="first" />
<documentationProvider implementation="siosio.OpenMavenCentralProvider" order="first" />
</extensions>

Expand Down

0 comments on commit b0ba003

Please sign in to comment.