-
Notifications
You must be signed in to change notification settings - Fork 0
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
211 additions
and
27 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/com/github/wenpiner/flutterassets/completion/DartCompletionContributor.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,16 @@ | ||
package com.github.wenpiner.flutterassets.completion | ||
|
||
import com.intellij.codeInsight.completion.CompletionContributor | ||
import com.intellij.codeInsight.completion.CompletionType | ||
import com.intellij.openapi.project.DumbAware | ||
import com.intellij.patterns.PlatformPatterns | ||
import com.jetbrains.lang.dart.DartTokenTypes | ||
|
||
class DartCompletionContributor : CompletionContributor(), DumbAware { | ||
init { | ||
// 在这里添加自定义的CompletionProvider | ||
extend(CompletionType.BASIC, PlatformPatterns. | ||
and(PlatformPatterns.psiElement(DartTokenTypes.REGULAR_STRING_PART),PlatformPatterns.not(PlatformPatterns.psiElement(DartTokenTypes.REGULAR_STRING_PART).inside(PlatformPatterns.psiElement(DartTokenTypes.IMPORT_STATEMENT)))) | ||
, DartCompletionProvider()) | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/kotlin/com/github/wenpiner/flutterassets/completion/DartCompletionProvider.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,41 @@ | ||
package com.github.wenpiner.flutterassets.completion | ||
|
||
import com.github.wenpiner.flutterassets.util.findPubspecYamlFile | ||
import com.github.wenpiner.flutterassets.util.getFlutterAssets | ||
import com.github.wenpiner.flutterassets.util.readInputStream | ||
import com.intellij.codeInsight.completion.CompletionParameters | ||
import com.intellij.codeInsight.completion.CompletionProvider | ||
import com.intellij.codeInsight.completion.CompletionResultSet | ||
import com.intellij.codeInsight.completion.CompletionUtilCore.DUMMY_IDENTIFIER | ||
import com.intellij.codeInsight.completion.PlainPrefixMatcher | ||
import com.intellij.codeInsight.lookup.LookupElementBuilder | ||
import com.intellij.openapi.progress.ProgressManager | ||
import com.intellij.refactoring.suggested.startOffset | ||
import com.intellij.util.ProcessingContext | ||
import io.ktor.utils.io.core.* | ||
import kotlin.io.use | ||
|
||
class DartCompletionProvider : CompletionProvider<CompletionParameters>() { | ||
override fun addCompletions( | ||
parameters: CompletionParameters, | ||
context: ProcessingContext, | ||
result: CompletionResultSet | ||
) { | ||
ProgressManager.checkCanceled() | ||
val caretPosition = parameters.position.text.indexOf(DUMMY_IDENTIFIER) | ||
val prefix = parameters.position.text.substring(0, caretPosition) | ||
findPubspecYamlFile(parameters.position.project, parameters.originalFile.virtualFile)?.let { | ||
val assets = it.inputStream.use { inputStream -> | ||
readInputStream(inputStream) | ||
} | ||
val flutterAssets = getFlutterAssets(assets) | ||
var r = result.withPrefixMatcher(PlainPrefixMatcher(prefix, false)).caseInsensitive() | ||
for (flutterAsset in flutterAssets) { | ||
ProgressManager.checkCanceled(); | ||
if (flutterAsset.startsWith(prefix)) { | ||
result.addElement(LookupElementBuilder.create(flutterAsset)) | ||
} | ||
} | ||
} | ||
} | ||
} |
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
110 changes: 110 additions & 0 deletions
110
src/main/kotlin/com/github/wenpiner/flutterassets/util/YamlUtil.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,110 @@ | ||
package com.github.wenpiner.flutterassets.util | ||
|
||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.vfs.VirtualFile | ||
import com.jetbrains.lang.dart.util.PubspecYamlUtil | ||
import org.jetbrains.annotations.NotNull | ||
import java.io.BufferedReader | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.io.InputStreamReader | ||
|
||
|
||
fun findPubspecYamlFile(@NotNull project: Project, @NotNull currentFile: VirtualFile): VirtualFile? { | ||
return PubspecYamlUtil.findPubspecYamlFile(project, currentFile) | ||
} | ||
|
||
fun readInputStream(inputStream: InputStream): List<String> { | ||
val lines: MutableList<String> = ArrayList() | ||
try { | ||
BufferedReader(InputStreamReader(inputStream)).use { reader -> | ||
var line: String? | ||
while (!reader.readLine().also { line = it }.isNullOrBlank()) { | ||
lines.add(line ?: "") | ||
} | ||
} | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
} | ||
return lines | ||
} | ||
|
||
fun getFlutterAssets(oldTexts: List<String>): List<String> { | ||
var flutterBool = false; | ||
var assetsBool = false; | ||
val assets = mutableListOf<String>() | ||
for (oldText in oldTexts) { | ||
// 判断当前是否为Flutter节点 | ||
if (!flutterBool && oldText.startsWith("flutter:")) { | ||
flutterBool = true; | ||
continue | ||
} | ||
// 判断当前是否为assets节点 | ||
if (!assetsBool && oldText.trim().contains("assets:") && flutterBool) { | ||
assetsBool = true; | ||
continue | ||
} | ||
// 如果找到assets节点 | ||
if (flutterBool && assetsBool) { | ||
// 写入新的数据 | ||
if (oldText.trim().startsWith("-")) { | ||
assets.add(oldText.trim().substring(1).trim()) | ||
} | ||
} | ||
} | ||
return assets | ||
} | ||
|
||
fun writeStrings(oldTexts: List<String>, newItems: List<String>): List<String> { | ||
// 新的数据 | ||
val newTexts = mutableListOf<String>() | ||
|
||
// 是否找到flutter节点 | ||
var flutterBool = false; | ||
// 是否找到assets节点 | ||
var assetsBool = false; | ||
var write = false; | ||
for (oldText in oldTexts) { | ||
// 判断当前是否为Flutter节点 | ||
if (!flutterBool && oldText.startsWith("flutter:")) { | ||
flutterBool = true; | ||
newTexts.add("$oldText${System.lineSeparator()}") | ||
continue | ||
} | ||
// 判断当前是否为assets节点 | ||
if (!assetsBool && oldText.trim().contains("assets:") && flutterBool) { | ||
assetsBool = true; | ||
newTexts.add("$oldText${System.lineSeparator()}") | ||
continue | ||
} | ||
|
||
// 未找到Assets节点则直接写入注释 | ||
if (!assetsBool && !flutterBool && oldText.trim().startsWith("#")) { | ||
newTexts.add("$oldText${System.lineSeparator()}") | ||
continue | ||
} | ||
|
||
// 如果找到assets节点 | ||
if (flutterBool && assetsBool && !write) { | ||
// 写入新的数据 | ||
for (newItem in newItems) { | ||
newTexts.add(" - $newItem${System.lineSeparator()}") | ||
} | ||
write = true | ||
} else { | ||
newTexts.add("$oldText${System.lineSeparator()}") | ||
} | ||
} | ||
// 如果未找到flutter节点,则直接写入 | ||
if (!flutterBool) { | ||
newTexts.add("flutter: ${System.lineSeparator()}") | ||
} | ||
if (!assetsBool) { | ||
newTexts.add(" assets: ${System.lineSeparator()}") | ||
// 写入新的数据 | ||
for (newItem in newItems) { | ||
newTexts.add(" - $newItem${System.lineSeparator()}") | ||
} | ||
} | ||
return newTexts | ||
} |
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