-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add caret text overlay for types of xml container
- Loading branch information
Showing
7 changed files
with
476 additions
and
6 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
16 changes: 16 additions & 0 deletions
16
src/fr/adrienbrault/idea/symfony2plugin/codeInsight/caret/overlay/CaretTextOverlay.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,16 @@ | ||
package fr.adrienbrault.idea.symfony2plugin.codeInsight.caret.overlay; | ||
|
||
import com.intellij.openapi.vfs.VirtualFile; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* @author Daniel Espendiller <daniel@espendiller.net> | ||
*/ | ||
public interface CaretTextOverlay { | ||
|
||
@Nullable | ||
CaretTextOverlayElement getOverlay(@NotNull CaretTextOverlayArguments args); | ||
|
||
boolean accepts(@NotNull VirtualFile virtualFile); | ||
} |
48 changes: 48 additions & 0 deletions
48
...adrienbrault/idea/symfony2plugin/codeInsight/caret/overlay/CaretTextOverlayArguments.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,48 @@ | ||
package fr.adrienbrault.idea.symfony2plugin.codeInsight.caret.overlay; | ||
|
||
import com.intellij.openapi.editor.event.CaretEvent; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* @author Daniel Espendiller <daniel@espendiller.net> | ||
*/ | ||
public class CaretTextOverlayArguments { | ||
|
||
@NotNull | ||
private final CaretEvent caretEvent; | ||
|
||
@NotNull | ||
private final PsiFile psiFile; | ||
|
||
@NotNull | ||
private final PsiElement psiElement; | ||
|
||
public CaretTextOverlayArguments(@NotNull CaretEvent caretEvent, @NotNull PsiFile psiFile, @NotNull PsiElement psiElement) { | ||
this.caretEvent = caretEvent; | ||
this.psiFile = psiFile; | ||
this.psiElement = psiElement; | ||
} | ||
|
||
@NotNull | ||
public PsiFile getPsiFile() { | ||
return psiFile; | ||
} | ||
|
||
@NotNull | ||
public PsiElement getPsiElement() { | ||
return psiElement; | ||
} | ||
|
||
@NotNull | ||
public Project getProject() { | ||
return psiElement.getProject(); | ||
} | ||
|
||
@NotNull | ||
public CaretEvent getCaretEvent() { | ||
return caretEvent; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...r/adrienbrault/idea/symfony2plugin/codeInsight/caret/overlay/CaretTextOverlayElement.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,21 @@ | ||
package fr.adrienbrault.idea.symfony2plugin.codeInsight.caret.overlay; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* @author Daniel Espendiller <daniel@espendiller.net> | ||
*/ | ||
public class CaretTextOverlayElement { | ||
|
||
@NotNull | ||
private final String text; | ||
|
||
public CaretTextOverlayElement(@NotNull String text) { | ||
this.text = text; | ||
} | ||
|
||
@NotNull | ||
public String getText() { | ||
return text; | ||
} | ||
} |
157 changes: 157 additions & 0 deletions
157
.../adrienbrault/idea/symfony2plugin/codeInsight/caret/overlay/CaretTextOverlayListener.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,157 @@ | ||
package fr.adrienbrault.idea.symfony2plugin.codeInsight.caret.overlay; | ||
|
||
import com.intellij.openapi.application.ApplicationManager; | ||
import com.intellij.openapi.editor.Caret; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.editor.event.CaretEvent; | ||
import com.intellij.openapi.editor.event.CaretListener; | ||
import com.intellij.openapi.editor.ex.EditorEx; | ||
import com.intellij.openapi.project.DumbService; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiDocumentManager; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import fr.adrienbrault.idea.symfony2plugin.codeInsight.caret.overlay.component.CaretOverlayComponent; | ||
import fr.adrienbrault.idea.symfony2plugin.codeInsight.caret.overlay.provider.XmlServiceContainerCaretTextOverlay; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
|
||
/** | ||
* @author Daniel Espendiller <daniel@espendiller.net> | ||
*/ | ||
public class CaretTextOverlayListener implements CaretListener { | ||
|
||
private static CaretTextOverlay[] CONTRIBUTOR = new CaretTextOverlay[] { | ||
new XmlServiceContainerCaretTextOverlay() | ||
}; | ||
|
||
private Timer timer = null; | ||
private int startDelayMs = 250; | ||
|
||
@Override | ||
public void caretPositionChanged(final CaretEvent caretEvent) { | ||
|
||
this.clear(); | ||
final Editor editor = caretEvent.getEditor(); | ||
removeOverlays(editor); | ||
|
||
if(!(editor instanceof EditorEx)) { | ||
return; | ||
} | ||
|
||
final Project project = editor.getProject(); | ||
if(project == null || DumbService.getInstance(project).isDumb()) { | ||
return; | ||
} | ||
|
||
this.timer = new Timer(); | ||
this.timer.schedule(new TimerTask() { | ||
@Override | ||
public void run() { | ||
ApplicationManager.getApplication().runReadAction(new MyPsiElementRunnable(project, caretEvent, editor)); | ||
timer = null; | ||
} | ||
}, startDelayMs); | ||
} | ||
|
||
@Override | ||
public void caretAdded(CaretEvent caretEvent) { | ||
|
||
} | ||
|
||
@Override | ||
public void caretRemoved(CaretEvent caretEvent) { | ||
|
||
} | ||
|
||
private void invokeUiComponent(final @NotNull Editor editor, final @NotNull PsiElement psiElement, final @NotNull CaretTextOverlayElement overlayElement) { | ||
SwingUtilities.invokeLater(new Runnable() { | ||
@Override | ||
public void run() { | ||
CaretOverlayComponent component = new CaretOverlayComponent(editor, overlayElement.getText(), psiElement.getTextOffset(), psiElement.getLanguage()); | ||
editor.getContentComponent().add(component); | ||
|
||
JViewport viewpoint = ((EditorEx) editor).getScrollPane().getViewport(); | ||
component.setBounds(0, 0, viewpoint.getWidth(), viewpoint.getHeight()); | ||
} | ||
}); | ||
} | ||
|
||
private void removeOverlays(@NotNull Editor editor) { | ||
for (Component component : editor.getContentComponent().getComponents()) { | ||
if(component instanceof CaretOverlayComponent) { | ||
editor.getContentComponent().remove(component); | ||
} | ||
} | ||
} | ||
|
||
private void clear() { | ||
if(timer == null) { | ||
return; | ||
} | ||
|
||
timer.cancel(); | ||
timer.purge(); | ||
timer = null; | ||
} | ||
|
||
private class MyPsiElementRunnable implements Runnable { | ||
private final Project project; | ||
private final CaretEvent caretEvent; | ||
private final Editor editor; | ||
|
||
public MyPsiElementRunnable(Project project, CaretEvent caretEvent, Editor editor) { | ||
this.project = project; | ||
this.caretEvent = caretEvent; | ||
this.editor = editor; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
|
||
Caret caret = caretEvent.getCaret(); | ||
if(caret == null) { | ||
return; | ||
} | ||
|
||
if (DumbService.getInstance(project).isDumb()) { | ||
return; | ||
} | ||
|
||
final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); | ||
if(psiFile == null) { | ||
return; | ||
} | ||
|
||
CaretTextOverlayArguments args = null; | ||
|
||
for (CaretTextOverlay caretTextOverlay : CONTRIBUTOR) { | ||
if(!caretTextOverlay.accepts(psiFile.getVirtualFile())) { | ||
continue; | ||
} | ||
|
||
if(args == null) { | ||
PsiElement element = psiFile.findElementAt(caret.getOffset()); | ||
if (element == null) { | ||
return; | ||
} | ||
|
||
args = new CaretTextOverlayArguments(caretEvent, psiFile, element); | ||
} | ||
|
||
CaretTextOverlayElement overlay = caretTextOverlay.getOverlay(args); | ||
if(overlay == null) { | ||
continue; | ||
} | ||
|
||
invokeUiComponent(editor, args.getPsiElement(), overlay); | ||
|
||
return; | ||
} | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...brault/idea/symfony2plugin/codeInsight/caret/overlay/component/CaretOverlayComponent.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,61 @@ | ||
package fr.adrienbrault.idea.symfony2plugin.codeInsight.caret.overlay.component; | ||
|
||
import com.intellij.lang.Language; | ||
import com.intellij.lang.xml.XMLLanguage; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.editor.XmlHighlighterColors; | ||
import com.intellij.openapi.editor.colors.EditorFontType; | ||
import com.intellij.openapi.util.text.StringUtil; | ||
import com.jetbrains.php.lang.PhpLanguage; | ||
import com.jetbrains.php.lang.highlighter.PhpHighlightingData; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.yaml.YAMLHighlighter; | ||
import org.jetbrains.yaml.YAMLLanguage; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
|
||
/** | ||
* @author Daniel Espendiller <daniel@espendiller.net> | ||
*/ | ||
public class CaretOverlayComponent extends JComponent { | ||
|
||
private final Editor editor; | ||
private final String content; | ||
private final int offset; | ||
private final Language language; | ||
private int horizontalMargin = 20; | ||
|
||
public CaretOverlayComponent(@NotNull Editor editor, @NotNull String content, int offset, @NotNull Language language) { | ||
this.editor = editor; | ||
this.content = content; | ||
this.offset = offset; | ||
this.language = language; | ||
} | ||
|
||
@Override | ||
protected void paintComponent(Graphics g) { | ||
|
||
if(language == XMLLanguage.INSTANCE) { | ||
g.setColor( | ||
editor.getColorsScheme().getAttributes(XmlHighlighterColors.XML_COMMENT).getForegroundColor() | ||
); | ||
} else if(language == YAMLLanguage.INSTANCE) { | ||
g.setColor( | ||
editor.getColorsScheme().getAttributes(YAMLHighlighter.COMMENT).getForegroundColor() | ||
); | ||
} else if(language == PhpLanguage.INSTANCE) { | ||
g.setColor( | ||
editor.getColorsScheme().getAttributes(PhpHighlightingData.COMMENT).getForegroundColor() | ||
); | ||
} | ||
|
||
g.setFont(editor.getColorsScheme().getFont(EditorFontType.CONSOLE_ITALIC)); | ||
|
||
int verticalAlignment = editor.getLineHeight() - editor.getColorsScheme().getEditorFontSize(); | ||
|
||
int offset = editor.getDocument().getLineEndOffset(StringUtil.offsetToLineNumber(editor.getDocument().getCharsSequence(), this.offset)); | ||
Point point = editor.visualPositionToXY(editor.offsetToVisualPosition(offset)); | ||
g.drawString(content, point.x + horizontalMargin, point.y + editor.getLineHeight() - verticalAlignment); | ||
} | ||
} |
Oops, something went wrong.