-
-
Notifications
You must be signed in to change notification settings - Fork 12
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
Peter Vanusanik
committed
Mar 19, 2023
1 parent
d993ca6
commit 5ba9457
Showing
10 changed files
with
468 additions
and
9 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
29 changes: 29 additions & 0 deletions
29
src/main/java/com/en_circle/slt/plugin/SltHyperspecWindowFactory.java
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,29 @@ | ||
package com.en_circle.slt.plugin; | ||
|
||
import com.en_circle.slt.plugin.ui.SltHyperspecView; | ||
import com.en_circle.slt.plugin.ui.SltUIService; | ||
import com.intellij.openapi.project.DumbAware; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.util.Disposer; | ||
import com.intellij.openapi.wm.ToolWindow; | ||
import com.intellij.openapi.wm.ToolWindowFactory; | ||
import com.intellij.ui.content.Content; | ||
import com.intellij.ui.content.ContentFactory; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class SltHyperspecWindowFactory implements ToolWindowFactory, DumbAware { | ||
|
||
@Override | ||
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) { | ||
{ | ||
SltHyperspecView hyperspecView = new SltHyperspecView(toolWindow); | ||
ContentFactory contentFactory = ContentFactory.getInstance(); | ||
Content content = contentFactory.createContent(hyperspecView.getContent(), | ||
SltBundle.message("slt.ui.clhs.title"), false); | ||
toolWindow.getContentManager().addContent(content); | ||
|
||
Disposer.register(SltUIService.getInstance(project), hyperspecView); | ||
} | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/com/en_circle/slt/plugin/actions/OpenCLHSAction.java
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,64 @@ | ||
package com.en_circle.slt.plugin.actions; | ||
|
||
import com.en_circle.slt.plugin.SltCommonLispFileType; | ||
import com.en_circle.slt.plugin.lisp.psi.LispSymbol; | ||
import com.en_circle.slt.plugin.services.SltProjectService; | ||
import com.intellij.openapi.actionSystem.ActionUpdateThread; | ||
import com.intellij.openapi.actionSystem.AnAction; | ||
import com.intellij.openapi.actionSystem.AnActionEvent; | ||
import com.intellij.openapi.actionSystem.CommonDataKeys; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.psi.PsiDocumentManager; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Objects; | ||
|
||
public class OpenCLHSAction extends AnAction { | ||
|
||
@Override | ||
public void actionPerformed(@NotNull AnActionEvent event) { | ||
Editor editor = event.getData(CommonDataKeys.EDITOR); | ||
if (editor != null && event.getProject() != null) { | ||
PsiFile file = PsiDocumentManager.getInstance(Objects.requireNonNull(editor.getProject())).getPsiFile(editor.getDocument()); | ||
if (file != null && SltCommonLispFileType.INSTANCE.equals(file.getFileType())) { | ||
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); | ||
LispSymbol symbol = null; | ||
if (element instanceof LispSymbol lispSymbol) { | ||
symbol = lispSymbol; | ||
} else { | ||
symbol = PsiTreeUtil.getParentOfType(element, LispSymbol.class); | ||
if (symbol == null) { | ||
symbol = PsiTreeUtil.getChildOfType(element, LispSymbol.class); | ||
} | ||
} | ||
|
||
if (symbol != null) { | ||
SltProjectService.getInstance(editor.getProject()) | ||
.showCLHSSymbol(Objects.requireNonNull(symbol.getName()).toLowerCase()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void update(@NotNull AnActionEvent event) { | ||
event.getPresentation().setEnabledAndVisible(false); | ||
|
||
Editor editor = event.getData(CommonDataKeys.EDITOR); | ||
if (editor != null && event.getProject() != null) { | ||
PsiFile file = PsiDocumentManager.getInstance(Objects.requireNonNull(editor.getProject())).getPsiFile(editor.getDocument()); | ||
if (file != null && SltCommonLispFileType.INSTANCE.equals(file.getFileType())) { | ||
event.getPresentation().setEnabledAndVisible(true); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public @NotNull ActionUpdateThread getActionUpdateThread() { | ||
return ActionUpdateThread.BGT; | ||
} | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
src/main/java/com/en_circle/slt/plugin/services/SltProjectService.java
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,86 @@ | ||
package com.en_circle.slt.plugin.services; | ||
|
||
import com.en_circle.slt.plugin.SltBundle; | ||
import com.en_circle.slt.plugin.ui.SltHyperspecView; | ||
import com.intellij.openapi.application.ApplicationManager; | ||
import com.intellij.openapi.project.DumbAware; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.ui.Messages; | ||
import com.intellij.openapi.wm.ToolWindow; | ||
import com.intellij.openapi.wm.ToolWindowManager; | ||
import org.apache.commons.io.IOUtils; | ||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.jsoup.nodes.Element; | ||
import org.jsoup.select.Elements; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.net.URL; | ||
import java.nio.charset.Charset; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class SltProjectService implements DumbAware { | ||
private static final Logger log = LoggerFactory.getLogger(SltProjectService.class); | ||
|
||
public static SltProjectService getInstance(Project project) { | ||
return project.getService(SltProjectService.class); | ||
} | ||
|
||
private SltHyperspecView hyperspecView; | ||
private Map<String, String> symbolRefMap = null; | ||
private final Project project; | ||
|
||
public SltProjectService(Project project) { | ||
this.project = project; | ||
ApplicationManager.getApplication().executeOnPooledThread(this::loadHyperspec); | ||
} | ||
|
||
private void loadHyperspec() { | ||
try { | ||
Map<String, String> symbolParseMap = new HashMap<>(); | ||
|
||
String base = "http://www.lispworks.com/documentation/HyperSpec/Front/"; | ||
String html = IOUtils.toString(new URL("http://www.lispworks.com/documentation/HyperSpec/Front/X_AllSym.htm"), | ||
Charset.defaultCharset()); | ||
Document document = Jsoup.parse(html); | ||
Elements links = document.select("a[href]"); | ||
for (Element link : links) { | ||
if ("DEFINITION".equals(link.attr("REL"))) { | ||
String ref = link.text(); | ||
String page = base + link.attr("HREF"); | ||
symbolParseMap.put(ref, page); | ||
} | ||
} | ||
|
||
symbolRefMap = symbolParseMap; | ||
} catch (Exception e) { | ||
log.error(e.getMessage()); | ||
} | ||
} | ||
|
||
public void showCLHSSymbol(String symbolName) { | ||
if (symbolRefMap != null) { | ||
String url = symbolRefMap.get(symbolName); | ||
if (url != null) { | ||
ToolWindow toolWindow = ToolWindowManager.getInstance(project) | ||
.getToolWindow("CLHS"); | ||
assert toolWindow != null; | ||
toolWindow.show(() -> { | ||
if (hyperspecView != null) { | ||
hyperspecView.showUrl(url); | ||
} | ||
}); | ||
return; | ||
} | ||
} | ||
|
||
Messages.showInfoMessage(String.format(SltBundle.message("slt.ui.clhs.nosymbol.message"), symbolName), | ||
SltBundle.message("slt.ui.clhs.nosymbol.title")); | ||
} | ||
|
||
public void setHyperspecView(SltHyperspecView hyperspecView) { | ||
this.hyperspecView = hyperspecView; | ||
} | ||
} |
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
Oops, something went wrong.