Skip to content

Commit

Permalink
feat: support latest idea
Browse files Browse the repository at this point in the history
  • Loading branch information
jadepeng committed Feb 9, 2022
1 parent 572e734 commit 33e145d
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 73 deletions.
38 changes: 29 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,41 +1,61 @@
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.4.21'
id "idea"
}


apply from: "$rootDir/scripts/verifier.gradle"

group 'com.github.jadepeng.rainbowfart'
version '1.0.4'
version '1.0.6'

sourceCompatibility = 1.8
sourceCompatibility = javaVersion
targetCompatibility = javaVersion

repositories {
mavenLocal()
maven { url "https://maven-central.storage-download.googleapis.com/repos/central/data/" }
maven { url "https://maven.aliyun.com/nexus/content/groups/public/" }
maven { url "https://repo.eclipse.org/content/groups/releases/" }
maven { url "https://plugins.gradle.org/m2/" }
maven { url "https://www.jetbrains.com/intellij-repository/releases" }
maven { url "https://www.jetbrains.com/intellij-repository/snapshots" }
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testImplementation group: 'junit', name: 'junit', version: '4.12'
implementation('javazoom:jlayer:1.0.1')
implementation('com.squareup.okhttp3:okhttp:3.13.1')
implementation('commons-io:commons-io:2.7')
implementation('com.squareup.okhttp3:okhttp:4.9.3')
implementation('commons-io:commons-io:2.11.0')
}

apply plugin: 'org.jetbrains.intellij'
intellij {
version '2020.1.2'
// not update since and util on build
pluginName name

version ideaVersion
updateSinceUntilBuild = false
}

apply plugin: 'idea'
idea {
project {
jdkName = javaVersion
languageLevel = javaVersion
}
}

patchPluginXml {
changeNotes """
- 1.0.6
- 支持新版intellij
- 1.0.5
- fix(#6)
<br />
- 1.0.4
- 更新intellij版本
- 更新intellij版本
<br />
- 1.0.3
- 支持设置语音包类型: TTS(在线语音合成),Custom(自定义), Builtin(内置)
Expand All @@ -49,5 +69,5 @@ patchPluginXml {

pluginVerifier {
pluginFileName = "$rootProject.name-$version"
ides = ["IC-2018.1", "IC-2019.1", "IC-2020.1", "CL-2020.1"]
ides = ["IC-2018.1", "IC-2019.1", "IC-2020.1", "CL-2020.1", "IU-2021.3.1"]
}
4 changes: 4 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name="rainbow-fart"
org.gradle.parallel=true
ideaVersion=IU-213.6461.79
javaVersion=11
3 changes: 1 addition & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
rootProject.name = 'rainbow-fart'

rootProject.name = 'rainbow-fart'
6 changes: 6 additions & 0 deletions src/main/java/com/github/jadepeng/rainbowfart/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.jadepeng.rainbowfart;

public class Constants {
public static final String EVENT_PASTE = "ON_PASTE";
public static final String EVENT_COMMENTS = "ON_COMMENTS";
}
29 changes: 20 additions & 9 deletions src/main/java/com/github/jadepeng/rainbowfart/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.*;
Expand Down Expand Up @@ -80,7 +81,8 @@ public static void init(Manifest manifest) {
});

// build regex
String regex = String.join("|", keyword2Contributes.keySet().stream().map(s -> s.replaceAll("\\$|\\.|\\+|\\(|\\)|\\[|\\]", "\\$&")).collect(Collectors.toList()));
String regex = keyword2Contributes.keySet().stream().map(s -> s.replaceAll("\\$|\\.|\\+|\\(|\\)|\\[|\\]", "\\$&")).collect(
Collectors.joining("|"));
keywordPattern = Pattern.compile(regex);
}

Expand All @@ -94,8 +96,7 @@ private static void resetSchedulePool() {

public static String getBuiltinTtsText() {
try {
URL filePath = Context.class.getClassLoader().getResource("/default.json");
return IOUtils.toString(filePath.openStream(), "utf-8");
return IOUtils.toString(Context.class.getResourceAsStream("/default.json"), StandardCharsets.UTF_8);
} catch (IOException e) {
return "";
}
Expand All @@ -108,8 +109,8 @@ static String readVoicePackageJson(String name) throws IOException {
if (isCustomer) {
return FileUtils.readFileToString(Paths.get(settings.getCustomVoicePackage(), name).toFile(), "utf-8");
}
URL filePath = PluginStarter.class.getClassLoader().getResource(BUILD_IN_VOICE_PACKAGE + "/" + settings.getBuildinPackage() + "/" + name);
return IOUtils.toString(filePath.openStream(), "utf-8");
URL filePath = PluginStarter.class.getResource(BUILD_IN_VOICE_PACKAGE + "/" + settings.getBuildinPackage() + "/" + name);
return IOUtils.toString(filePath.openStream(), StandardCharsets.UTF_8);
}

/**
Expand All @@ -122,7 +123,7 @@ public static void loadConfig() {
if (!settings.isEnable()) {
return;
}
// TTS 使用配置里的数据
// TTS 使用配置里的数据 if if else h
String json = settings.getType() != VoicePackageType.TTS ? readVoicePackageJson("manifest.json") : settings.getTtsSettings().getResourceText();
if (StringUtils.isBlank(json)) {
json = getBuiltinTtsText();
Expand All @@ -139,7 +140,7 @@ public static void loadConfig() {
}
Context.init(manifest);

} catch (IOException e) {
} catch (IOException ignored) {
}
}

Expand Down Expand Up @@ -224,12 +225,22 @@ public static void play(List<Contribute> contributes) {
preparePlayThreadPool.submit(() -> Mp3Player.play(contributes));
}

public static boolean playCandidates(String keywords) {
List<Contribute> voices = getCandidate(keywords);
if (!voices.isEmpty()) {
Context.play(voices);
return true;
}
return false;
}

public static void onEvent(String event) {
System.out.println(event);
playCandidates(event);
}

public static void onEvent(String event, Object args) {
public static void onEvent(String event, String args) {
System.out.println(event + ":" + args);
playCandidates(args);
}

public static void main(String[] args) {
Expand Down

This file was deleted.

22 changes: 12 additions & 10 deletions src/main/java/com/github/jadepeng/rainbowfart/Mp3Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.github.jadepeng.rainbowfart.settings.FartSettings;
import com.github.jadepeng.rainbowfart.settings.VoicePackageType;
import com.google.common.util.concurrent.ThreadFactoryBuilder;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import org.apache.commons.io.IOUtils;

import java.io.*;
Expand Down Expand Up @@ -43,21 +45,20 @@ public class Mp3Player {
ThreadFactory playerFactory = new ThreadFactoryBuilder()
.setNameFormat("player-pool-%d").build();
playerTheadPool = new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(1024), playerFactory, new ThreadPoolExecutor.AbortPolicy());
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(1024), playerFactory,
new ThreadPoolExecutor.AbortPolicy());
}

static boolean tryLoadFromBuiltInCache(String cacheFile, File targetFile) {
URL cache = Context.class.getClassLoader().getResource("/cache/" + cacheFile);
URL cache = Context.class.getResource("/cache/" + cacheFile);
if (cache != null) {
try {
FileOutputStream outputStream = new FileOutputStream(targetFile);
IOUtils.copy(cache.openStream(), outputStream);
outputStream.close();
return true;
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
} catch (Exception ignored) {
}

}
Expand All @@ -67,7 +68,8 @@ static boolean tryLoadFromBuiltInCache(String cacheFile, File targetFile) {
static void playTTS(List<Contribute> contributes, FartSettings settings) {
List<String> texts = contributes.stream().flatMap(c -> c.getText().stream()).collect(Collectors.toList());
String text = texts.get(new Random().nextInt() % texts.size());
String cacheFileName = settings.getTtsSettings().getVcn() + (settings.getTtsSettings().getVcn() + text).hashCode() + ".mp3";
String cacheFileName =
settings.getTtsSettings().getVcn() + (settings.getTtsSettings().getVcn() + text).hashCode() + ".mp3";
File cacheFile = Paths.get(TEMP_TTS_CACHE_DIR, cacheFileName).toFile();
// If not exist, try online tts
if (!cacheFile.exists()) {
Expand Down Expand Up @@ -109,7 +111,8 @@ public static void play(List<Contribute> contributes) {
try {
InputStream inputStream = null;
if (settings.getType() == VoicePackageType.Builtin) {
inputStream = Context.class.getResourceAsStream(Context.BUILD_IN_VOICE_PACKAGE + "/" + settings.getBuildinPackage() + "/" + file);
inputStream = Context.class.getResourceAsStream(
Context.BUILD_IN_VOICE_PACKAGE + "/" + settings.getBuildinPackage() + "/" + file);
} else {
File mp3File = Paths.get(settings.getCustomVoicePackage(), file).toFile();
if (mp3File.exists()) {
Expand All @@ -125,8 +128,7 @@ public static void play(List<Contribute> contributes) {
if (settings.getType() == VoicePackageType.Custom) {
inputStream.close();
}
} catch (JavaLayerException e) {
} catch (IOException e) {
} catch (Exception ignored) {
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.jadepeng.rainbowfart.extensions;

import com.github.jadepeng.rainbowfart.Constants;
import com.github.jadepeng.rainbowfart.Context;
import com.intellij.codeInsight.editorActions.CommentCompleteHandler;
import com.intellij.lang.CodeDocumentationAwareCommenter;
Expand All @@ -15,7 +16,7 @@ public boolean isCommentComplete(PsiComment comment, CodeDocumentationAwareComme

@Override
public boolean isApplicable(PsiComment comment, CodeDocumentationAwareCommenter commenter) {
Context.onEvent("CommentComplete");
Context.onEvent(Constants.EVENT_COMMENTS);
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.jadepeng.rainbowfart.extensions;

import com.github.jadepeng.rainbowfart.Constants;
import com.github.jadepeng.rainbowfart.Context;
import com.intellij.codeInsight.editorActions.CopyPastePostProcessor;
import com.intellij.codeInsight.editorActions.TextBlockTransferableData;
Expand Down Expand Up @@ -44,7 +45,7 @@ public List<TextBlockTransferableData> collectTransferableData(PsiFile file, Edi
buffer.append(text.subSequence(lineStart, lineEnd));
}

Context.onEvent("onPaste", buffer.toString());
Context.onEvent(Constants.EVENT_PASTE, buffer.toString());

return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.actionSystem.TypedActionHandler;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -14,7 +15,7 @@
import java.util.List;

/**
* Typed Action Handler
* Typed Action Handler
*/
public class TypedHandler extends TypedActionHandlerBase {

Expand All @@ -26,22 +27,17 @@ public TypedHandler(@Nullable TypedActionHandler originalHandler) {
super(originalHandler);
}


@Override
public void execute(@NotNull Editor editor, char charTyped, @NotNull DataContext dataContext) {
inputHistory.add(String.valueOf(charTyped));
String str = StringUtils.join(inputHistory, "");
try {
List<Contribute> voices = Context.getCandidate(str);
if (!voices.isEmpty()) {
Context.play(voices);
if (Context.playCandidates(str)) {
inputHistory.clear();
}
}catch (Exception e){
// TODO
} catch (Exception e) {
inputHistory.clear();
}

if (this.myOriginalHandler != null) {
this.myOriginalHandler.execute(editor, charTyped, dataContext);
}
Expand Down
Loading

0 comments on commit 33e145d

Please sign in to comment.