mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
template language injection: use editor highlighter instead of lexer (IDEA-106449)
GitOrigin-RevId: 4cf75dc140a4758187fbda4b8205b936e7906c8c
This commit is contained in:
committed by
intellij-monorepo-bot
parent
7aa15dd139
commit
f7f666fbb9
+1
-17
@@ -19,17 +19,13 @@ package com.intellij.codeInsight.daemon.impl;
|
||||
import com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder;
|
||||
import com.intellij.concurrency.JobLauncher;
|
||||
import com.intellij.injected.editor.DocumentWindow;
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.lang.injection.InjectedLanguageManager;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.HighlighterColors;
|
||||
import com.intellij.openapi.editor.colors.EditorColors;
|
||||
import com.intellij.openapi.editor.colors.TextAttributesKey;
|
||||
import com.intellij.openapi.editor.markup.TextAttributes;
|
||||
import com.intellij.openapi.fileTypes.SyntaxHighlighter;
|
||||
import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory;
|
||||
import com.intellij.openapi.progress.ProcessCanceledException;
|
||||
import com.intellij.openapi.progress.ProgressIndicator;
|
||||
import com.intellij.openapi.progress.ProgressManager;
|
||||
@@ -46,7 +42,6 @@ import com.intellij.psi.PsiLanguageInjectionHost;
|
||||
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageManagerImpl;
|
||||
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
|
||||
import com.intellij.psi.impl.source.tree.injected.Place;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.util.CommonProcessors;
|
||||
import com.intellij.util.Processor;
|
||||
import com.intellij.util.Processors;
|
||||
@@ -378,9 +373,6 @@ public class InjectedGeneralHighlightingPass extends GeneralHighlightingPass {
|
||||
List<InjectedLanguageUtil.TokenInfo> tokens = InjectedLanguageUtil.getHighlightTokens(injectedPsi);
|
||||
if (tokens == null) return;
|
||||
|
||||
final Language injectedLanguage = injectedPsi.getLanguage();
|
||||
Project project = injectedPsi.getProject();
|
||||
SyntaxHighlighter syntaxHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(injectedLanguage, project, injectedPsi.getVirtualFile());
|
||||
final TextAttributes defaultAttrs = myGlobalScheme.getAttributes(HighlighterColors.TEXT);
|
||||
|
||||
Place shreds = InjectedLanguageUtil.getShreds(injectedPsi);
|
||||
@@ -388,7 +380,6 @@ public class InjectedGeneralHighlightingPass extends GeneralHighlightingPass {
|
||||
int injectionHostTextRangeStart = -1;
|
||||
for (InjectedLanguageUtil.TokenInfo token : tokens) {
|
||||
ProgressManager.checkCanceled();
|
||||
IElementType tokenType = token.type;
|
||||
TextRange range = token.rangeInsideInjectionHost;
|
||||
if (range.getLength() == 0) continue;
|
||||
if (shredIndex != token.shredIndex) {
|
||||
@@ -400,15 +391,8 @@ public class InjectedGeneralHighlightingPass extends GeneralHighlightingPass {
|
||||
}
|
||||
TextRange annRange = range.shiftRight(injectionHostTextRangeStart);
|
||||
|
||||
TextAttributesKey[] keys = syntaxHighlighter.getTokenHighlights(tokenType);
|
||||
// force attribute colors to override host' ones
|
||||
TextAttributes attributes = null;
|
||||
for(TextAttributesKey key:keys) {
|
||||
TextAttributes attrs2 = myGlobalScheme.getAttributes(key);
|
||||
if (attrs2 != null) {
|
||||
attributes = attributes == null ? attrs2 : TextAttributes.merge(attributes, attrs2);
|
||||
}
|
||||
}
|
||||
TextAttributes attributes = token.attributes;
|
||||
TextAttributes forcedAttributes;
|
||||
if (attributes == null || attributes.isEmpty() || attributes.equals(defaultAttrs)) {
|
||||
forcedAttributes = TextAttributes.ERASE_MARKER;
|
||||
|
||||
+7
-1
@@ -16,6 +16,7 @@ import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.ex.DocumentEx;
|
||||
import com.intellij.openapi.editor.impl.EditorImpl;
|
||||
import com.intellij.openapi.editor.markup.TextAttributes;
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager;
|
||||
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
|
||||
import com.intellij.openapi.progress.ProgressIndicator;
|
||||
@@ -82,11 +83,16 @@ public class InjectedLanguageUtil {
|
||||
@NotNull public final IElementType type;
|
||||
@NotNull public final ProperTextRange rangeInsideInjectionHost;
|
||||
public final int shredIndex;
|
||||
public final TextAttributes attributes;
|
||||
|
||||
public TokenInfo(@NotNull IElementType type, @NotNull ProperTextRange rangeInsideInjectionHost, int shredIndex) {
|
||||
public TokenInfo(@NotNull IElementType type,
|
||||
@NotNull ProperTextRange rangeInsideInjectionHost,
|
||||
int shredIndex,
|
||||
@NotNull TextAttributes attributes) {
|
||||
this.type = type;
|
||||
this.rangeInsideInjectionHost = rangeInsideInjectionHost;
|
||||
this.shredIndex = shredIndex;
|
||||
this.attributes = attributes;
|
||||
}
|
||||
}
|
||||
static void setHighlightTokens(@NotNull PsiFile file, @NotNull List<TokenInfo> tokens) {
|
||||
|
||||
+19
-9
@@ -7,12 +7,16 @@ import com.intellij.injected.editor.DocumentWindow;
|
||||
import com.intellij.injected.editor.VirtualFileWindow;
|
||||
import com.intellij.lang.*;
|
||||
import com.intellij.lang.injection.MultiHostRegistrar;
|
||||
import com.intellij.lexer.Lexer;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.colors.EditorColorsManager;
|
||||
import com.intellij.openapi.editor.colors.EditorColorsScheme;
|
||||
import com.intellij.openapi.editor.ex.DocumentEx;
|
||||
import com.intellij.openapi.editor.highlighter.EditorHighlighter;
|
||||
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
|
||||
import com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl;
|
||||
import com.intellij.openapi.fileTypes.SyntaxHighlighter;
|
||||
import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory;
|
||||
import com.intellij.openapi.fileTypes.EditorHighlighterProvider;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.fileTypes.FileTypeEditorHighlighterProviders;
|
||||
import com.intellij.openapi.progress.ProcessCanceledException;
|
||||
import com.intellij.openapi.progress.ProgressIndicator;
|
||||
import com.intellij.openapi.project.Project;
|
||||
@@ -750,9 +754,13 @@ class InjectionRegistrarImpl extends MultiHostRegistrarImpl implements MultiHost
|
||||
@NotNull VirtualFileWindow virtualFile,
|
||||
@NotNull Project project,
|
||||
@NotNull List<? extends PlaceInfo> placeInfos) {
|
||||
SyntaxHighlighter syntaxHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(language, project, (VirtualFile)virtualFile);
|
||||
Lexer lexer = syntaxHighlighter.getHighlightingLexer();
|
||||
lexer.start(outChars);
|
||||
VirtualFile file = (VirtualFile)virtualFile;
|
||||
FileType fileType = file.getFileType();
|
||||
EditorHighlighterProvider provider = FileTypeEditorHighlighterProviders.INSTANCE.forFileType(fileType);
|
||||
EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
|
||||
EditorHighlighter highlighter = provider.getEditorHighlighter(project, fileType, file, scheme);
|
||||
highlighter.setText(outChars);
|
||||
HighlighterIterator iterator = highlighter.createIterator(0);
|
||||
int hostNum = -1;
|
||||
int prevHostEndOffset = 0;
|
||||
LiteralTextEscaper escaper = null;
|
||||
@@ -761,8 +769,9 @@ class InjectionRegistrarImpl extends MultiHostRegistrarImpl implements MultiHost
|
||||
TextRange rangeInsideHost = null;
|
||||
int shredEndOffset = -1;
|
||||
List<InjectedLanguageUtil.TokenInfo> tokens = new ArrayList<>(outChars.length()/5); // avg. token per 5 chars
|
||||
for (IElementType tokenType = lexer.getTokenType(); tokenType != null; lexer.advance(), tokenType = lexer.getTokenType()) {
|
||||
TextRange range = new ProperTextRange(lexer.getTokenStart(), lexer.getTokenEnd());
|
||||
while (!iterator.atEnd()) {
|
||||
IElementType tokenType = iterator.getTokenType();
|
||||
TextRange range = new ProperTextRange(iterator.getStart(), iterator.getEnd());
|
||||
while (range != null && !range.isEmpty()) {
|
||||
if (range.getStartOffset() >= shredEndOffset) {
|
||||
hostNum++;
|
||||
@@ -792,10 +801,11 @@ class InjectionRegistrarImpl extends MultiHostRegistrarImpl implements MultiHost
|
||||
prevHostEndOffset = shredEndOffset;
|
||||
}
|
||||
ProperTextRange rangeInHost = new ProperTextRange(start, end);
|
||||
tokens.add(new InjectedLanguageUtil.TokenInfo(tokenType, rangeInHost,hostNum));
|
||||
tokens.add(new InjectedLanguageUtil.TokenInfo(tokenType, rangeInHost, hostNum, iterator.getTextAttributes()));
|
||||
}
|
||||
range = spilled;
|
||||
}
|
||||
iterator.advance();
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user