Skip to content

Commit

Permalink
First
Browse files Browse the repository at this point in the history
  • Loading branch information
Wenpiner committed Oct 7, 2023
1 parent 45b2436 commit db49738
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [ ] AI intelligent suggestions.


![demo](https://github.com/Wenpiner/FlutterAssets/blob/main/images/g.gif)
## Installation

- Using the IDE built-in plugin system:
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ intellij {
type = properties("platformType")

// Plugin Dependencies. Uses `platformPlugins` property from the gradle.properties file.
// plugins = properties("platformPlugins").map { it.split(',').map(String::trim).filter(String::isNotEmpty) }
plugins = properties("platformPlugins").map { it.split(',').map(String::trim).filter(String::isNotEmpty) }
}

Expand Down
7 changes: 4 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ pluginGroup = com.github.wenpiner.flutterassets
pluginName = FlutterAssets
pluginRepositoryUrl = https://github.com/Wenpiner/FlutterAssets
# SemVer format -> https://semver.org
pluginVersion = 0.0.2
pluginVersion = 0.0.4

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 223
pluginUntilBuild = 233.*

# IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension
platformType = IC
platformType = IU
platformVersion = 2023.2.1

# Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
# Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22
platformPlugins =
platformPlugins = Dart:232.9559.10

# Gradle Releases -> https://github.com/gradle/gradle/releases
gradleVersion = 8.4
Expand All @@ -32,3 +32,4 @@ org.gradle.caching = true

# Enable Gradle Kotlin DSL Lazy Property Assignment -> https://docs.gradle.org/current/userguide/kotlin_dsl.html#kotdsl:assignment
systemProp.org.gradle.unsafe.kotlin.assignment = true
dartVersion = 232.10072.19
Binary file added images/g.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ class FlutterAssetsAction : AnAction() {
e.presentation.isEnabledAndVisible = false
return
}

// 获取项目目录
val projectPath = e.project!!.basePath ?: return
// 判断是否存在pubspec.yaml
val pubspec = File("$projectPath/pubspec.yaml")
if (!pubspec.exists() || !pubspec.isFile) {
e.presentation.isEnabledAndVisible = false
Expand Down
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())
}
}
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

Check warning on line 13 in src/main/kotlin/com/github/wenpiner/flutterassets/completion/DartCompletionProvider.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import com.intellij.util.ProcessingContext
import io.ktor.utils.io.core.*

Check warning on line 15 in src/main/kotlin/com/github/wenpiner/flutterassets/completion/DartCompletionProvider.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
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();

Check warning on line 34 in src/main/kotlin/com/github/wenpiner/flutterassets/completion/DartCompletionProvider.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
if (flutterAsset.startsWith(prefix)) {
result.addElement(LookupElementBuilder.create(flutterAsset))
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.github.wenpiner.flutterassets.services

import com.github.wenpiner.flutterassets.util.readInputStream
import com.github.wenpiner.flutterassets.util.writeStrings
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.components.Service
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.project.Project
import org.yaml.snakeyaml.DumperOptions
import org.yaml.snakeyaml.Yaml
import java.io.File
import com.intellij.openapi.vfs.LocalFileSystem
import org.apache.tools.ant.filters.StringInputStream

Check warning on line 10 in src/main/kotlin/com/github/wenpiner/flutterassets/services/FlutterAssetsService.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import java.io.*


@Service(Service.Level.PROJECT)
Expand All @@ -20,6 +23,8 @@ class FlutterAssetsService(private var project: Project) {

}



class MyTask(private var project: Project, private var relativePath: String) : Runnable {
private var result = false
override fun run() {
Expand All @@ -29,26 +34,28 @@ class MyTask(private var project: Project, private var relativePath: String) : R
result = false
return
}
val options = DumperOptions()
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK)
options.indent = 2
options.indicatorIndent = 2
options.indentWithIndicator = true
val findFileByIoFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(pubspec)
findFileByIoFile ?: return
findFileByIoFile.refresh(false, true)
WriteCommandAction.runWriteCommandAction(project, object : Runnable {

Check notice on line 40 in src/main/kotlin/com/github/wenpiner/flutterassets/services/FlutterAssetsService.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Object literal can be converted to lambda

Convert to lambda
override fun run() {
val outputStream = findFileByIoFile.getOutputStream("FlutterAssetsService")
// 根据inputStream获取所有行
val inputStream = findFileByIoFile.inputStream
val allLines = inputStream.use {
readInputStream(inputStream)
}
val writeStrings = writeStrings(allLines, flutterAssets)

val yaml = Yaml(options)
val yamlMap: MutableMap<String, Any> = yaml.load(pubspec.readText())

val flutterKey = yamlMap.containsKey("flutter")
val flutter = yamlMap["flutter"];
if (!flutterKey || flutter == null) {
yamlMap["flutter"] = mapOf("assets" to flutterAssets)
} else {
val tempMap = flutter as MutableMap<Any, Any>
tempMap["assets"] = flutterAssets
}
outputStream.use { it ->

Check notice on line 50 in src/main/kotlin/com/github/wenpiner/flutterassets/services/FlutterAssetsService.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant lambda arrow

Redundant lambda arrow
val str = writeStrings.joinToString("")
it.write(str.toByteArray())
it.flush()
}
findFileByIoFile.refresh(true, true)
}
});

Check warning on line 57 in src/main/kotlin/com/github/wenpiner/flutterassets/services/FlutterAssetsService.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon

val s = yaml.dump(yamlMap)
pubspec.writeText(s)
result = true
}

Expand All @@ -70,4 +77,6 @@ class MyTask(private var project: Project, private var relativePath: String) : R
}
return flutterAssets
}


}
110 changes: 110 additions & 0 deletions src/main/kotlin/com/github/wenpiner/flutterassets/util/YamlUtil.kt
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;

Check warning on line 33 in src/main/kotlin/com/github/wenpiner/flutterassets/util/YamlUtil.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
var assetsBool = false;

Check warning on line 34 in src/main/kotlin/com/github/wenpiner/flutterassets/util/YamlUtil.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
val assets = mutableListOf<String>()
for (oldText in oldTexts) {
// 判断当前是否为Flutter节点
if (!flutterBool && oldText.startsWith("flutter:")) {
flutterBool = true;

Check warning on line 39 in src/main/kotlin/com/github/wenpiner/flutterassets/util/YamlUtil.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
continue
}
// 判断当前是否为assets节点
if (!assetsBool && oldText.trim().contains("assets:") && flutterBool) {
assetsBool = true;

Check warning on line 44 in src/main/kotlin/com/github/wenpiner/flutterassets/util/YamlUtil.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
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;

Check warning on line 63 in src/main/kotlin/com/github/wenpiner/flutterassets/util/YamlUtil.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
// 是否找到assets节点
var assetsBool = false;

Check warning on line 65 in src/main/kotlin/com/github/wenpiner/flutterassets/util/YamlUtil.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
var write = false;

Check warning on line 66 in src/main/kotlin/com/github/wenpiner/flutterassets/util/YamlUtil.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
for (oldText in oldTexts) {
// 判断当前是否为Flutter节点
if (!flutterBool && oldText.startsWith("flutter:")) {
flutterBool = true;

Check warning on line 70 in src/main/kotlin/com/github/wenpiner/flutterassets/util/YamlUtil.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
newTexts.add("$oldText${System.lineSeparator()}")
continue
}
// 判断当前是否为assets节点
if (!assetsBool && oldText.trim().contains("assets:") && flutterBool) {
assetsBool = true;

Check warning on line 76 in src/main/kotlin/com/github/wenpiner/flutterassets/util/YamlUtil.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
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
}
8 changes: 8 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<vendor>wenpiner</vendor>

<depends>com.intellij.modules.platform</depends>
<depends>Dart</depends>

<change-notes>
First release
Expand All @@ -19,4 +20,11 @@
<add-to-group group-id="Flutter Assets"/>
</action>
</actions>

<extensions defaultExtensionNs="com.intellij">
<completion.contributor
language="Dart"
order="first"
implementationClass="com.github.wenpiner.flutterassets.completion.DartCompletionContributor"/>
</extensions>
</idea-plugin>

0 comments on commit db49738

Please sign in to comment.