Skip to content

Commit

Permalink
fix: next and previous line action
Browse files Browse the repository at this point in the history
  • Loading branch information
muedsa committed Dec 7, 2021
1 parent 7d7e57c commit 366277a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'com.muedsa.intellij.text-reader-sidebar-tool'
version '2021.12.06'
version '2021.12.07'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ public class NextLineAction extends AnAction {
public void actionPerformed(AnActionEvent e) {
ReaderWindow readerWindow = ReaderWindowHolder.get(e.getProject());
if(readerWindow == null){
Notification.sendHiddenNotify(e.getProject(), "未初始化ToolWindow, 请点击工具栏打开一次窗口", NotificationType.INFORMATION);
Notification.sendHiddenNotify(e.getProject(), "未初始化ToolWindow, 请点击工具栏打开一次窗口", NotificationType.WARNING);
}else{
Notification.sendHiddenNotify(e.getProject(), readerWindow.nextLine(), NotificationType.INFORMATION);
if(readerWindow.isReadyLineAction()){
Notification.sendHiddenNotify(e.getProject(), readerWindow.nextLine(), NotificationType.INFORMATION);
}else{
Notification.sendHiddenNotify(e.getProject(), "请选择文件载入或清除筛选项", NotificationType.WARNING);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ public void actionPerformed(@NotNull AnActionEvent e) {
if(readerWindow == null){
Notification.sendHiddenNotify(e.getProject(), "未初始化ToolWindow, 请点击工具栏打开一次窗口", NotificationType.INFORMATION);
}else{
Notification.sendHiddenNotify(e.getProject(), readerWindow.previousLine(), NotificationType.INFORMATION);
if(readerWindow.isReadyLineAction()){
Notification.sendHiddenNotify(e.getProject(), readerWindow.previousLine(), NotificationType.INFORMATION);
}else{
Notification.sendHiddenNotify(e.getProject(), "请选择文件载入或清除筛选项", NotificationType.WARNING);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -348,22 +348,25 @@ private void updateParagraphSpace(){
setTextContent();
}

private void setTextContent(){
private boolean setTextContent(){
Chapter chapter = titleList.getSelectedValue();
boolean flag = false;
if(chapter != null){
String text = "";
try{
text = ChapterUtil.formatChapterContent(textFile, chapter);
textContent.setText(text);
noBlankChapterText = text.replaceFirst("\n", "##").replaceAll("\\s*", "");
textContent.setCaretPosition(0);
positionInChapter = 0;
flag = true;
}
catch (IOException e){
e.printStackTrace();
sendNotify("文件读取错误", e.getLocalizedMessage(), NotificationType.ERROR);
}
textContent.setText(text);
noBlankChapterText = text.replaceFirst("\n", "##").replaceAll("\\s*", "");
textContent.setCaretPosition(0);
positionInChapter = 0;
}
return flag;
}

private void sendNotify(String title, String content, NotificationType type){
Expand All @@ -376,7 +379,10 @@ public String nextLine(){
int lineSize = (int)lineSizeSpinner.getValue();
if(lineSize > 0){
if(StringUtils.isEmpty(noBlankChapterText) || positionInChapter > noBlankChapterText.length()){
nextChapter();
if(!nextChapter()){
line = "读取下一章失败!";
return line;
}
}
line = StringUtils.mid(noBlankChapterText, positionInChapter, lineSize);
positionInChapter += lineSize;
Expand All @@ -396,8 +402,13 @@ public String previousLine(){
if(toolWindow.isAvailable()){
int lineSize = (int)lineSizeSpinner.getValue();
if(lineSize > 0){
if(positionInChapter == 0){
previousChapter();
if(positionInChapter - lineSize == 0){
if(!previousChapter()){
line = "读取上一章失败!";
positionInChapter = 0;
return line;
}
positionInChapter = noBlankChapterText.length() - lineSize;
}else{
positionInChapter -= lineSize * 2;
if(positionInChapter < 0){
Expand All @@ -412,20 +423,28 @@ public String previousLine(){
return line;
}

private void nextChapter(){
private boolean nextChapter(){
boolean flag = false;
int count = titleList.getItemsCount();
int index = titleList.getSelectedIndex() + 1;
if(index + 1 <= count){
titleList.setSelectedIndex(index);
setTextContent();
flag = setTextContent();
}
return flag;
}

private void previousChapter(){
private boolean previousChapter(){
boolean flag = false;
int index = titleList.getSelectedIndex() - 1;
if(index >= 0){
titleList.setSelectedIndex(index);
setTextContent();
flag = setTextContent();
}
return flag;
}

public boolean isReadyLineAction(){
return !titleList.isEmpty();
}
}
5 changes: 5 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
]]></description>

<change-notes><![CDATA[
<h3>2021.12.07</h3>
<ul>
<li>fix: next and previous line action</li>
</ul>
<hr/>
<h3>2021.12.06</h3>
<ul>
<li>lower idea version: 2020.1</li>
Expand Down

0 comments on commit 366277a

Please sign in to comment.