PY-61639 Move PyEditorHighlighterProvider to python.syntax

GitOrigin-RevId: 22d15ca7ba81faa698f93da308ea1836bdb2b434
This commit is contained in:
Petr
2024-02-10 15:31:00 +01:00
committed by intellij-monorepo-bot
parent b455ab1512
commit e96055cb6d
4 changed files with 1 additions and 1 deletions

View File

@@ -5,5 +5,6 @@
<quoteHandler fileType="Python" className="com.jetbrains.python.editor.PythonQuoteHandler"/>
<langCodeStyleSettingsProvider implementation="com.jetbrains.python.formatter.PyLanguageCodeStyleSettingsProvider"/>
<lang.braceMatcher language="Python" implementationClass="com.jetbrains.python.PyBraceMatcher"/>
<editorHighlighterProvider filetype="Python" implementationClass="com.jetbrains.python.PyEditorHighlighterProvider"/>
</extensions>
</idea-plugin>

View File

@@ -0,0 +1,21 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.jetbrains.python;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.highlighter.EditorHighlighter;
import com.intellij.openapi.fileTypes.EditorHighlighterProvider;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.python.highlighting.PythonEditorHighlighter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class PyEditorHighlighterProvider implements EditorHighlighterProvider {
@Override
public EditorHighlighter getEditorHighlighter(@Nullable Project project,
@NotNull FileType fileType, @Nullable VirtualFile virtualFile,
@NotNull EditorColorsScheme colors) {
return new PythonEditorHighlighter(colors, project, virtualFile);
}
}

View File

@@ -0,0 +1,77 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.jetbrains.python.highlighting;
import com.intellij.lexer.LayeredLexer;
import com.intellij.lexer.Lexer;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.ex.util.LexerEditorHighlighter;
import com.intellij.openapi.editor.highlighter.HighlighterClient;
import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.PythonFileType;
import com.jetbrains.python.lexer.PythonHighlightingLexer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User : catherine
*/
public class PythonEditorHighlighter extends LexerEditorHighlighter {
public PythonEditorHighlighter(@NotNull EditorColorsScheme scheme, @Nullable Project project, @Nullable VirtualFile file) {
super(SyntaxHighlighterFactory.getSyntaxHighlighter(file != null ? file.getFileType() : PythonFileType.INSTANCE,
project,
file),
scheme);
}
private Boolean hadUnicodeImport = false;
public static final Key<Boolean> KEY = new Key<>("python.future.import");
@Override
public void documentChanged(@NotNull DocumentEvent e) {
synchronized (this) {
final Document document = e.getDocument();
Lexer l = getLexer();
// if the document been changed before "from __future__ import unicode_literals"
// we should update the whole document
if (l instanceof LayeredLexer) {
Lexer delegate = ((LayeredLexer)l).getDelegate();
int offset = e.getOffset();
int lineNumber = document.getLineNumber(offset);
TextRange tr = new TextRange(document.getLineStartOffset(lineNumber), document.getLineEndOffset(lineNumber));
document.putUserData(KEY, !document.getText(tr).contains(PyNames.UNICODE_LITERALS));
Boolean hasUnicodeImport = document.getUserData(KEY);
if (delegate instanceof PythonHighlightingLexer &&
(((PythonHighlightingLexer)delegate).getImportOffset() > e.getOffset()
|| hasUnicodeImport != hadUnicodeImport)) {
((PythonHighlightingLexer)delegate).clearState(e.getDocument().getTextLength());
setText(document.getCharsSequence());
}
else super.documentChanged(e);
}
else super.documentChanged(e);
}
}
@Override
public void beforeDocumentChange(@NotNull DocumentEvent e) {
final Document document = e.getDocument();
hadUnicodeImport = document.getUserData(KEY);
}
@Override
public void setEditor(@NotNull HighlighterClient editor) {
Lexer l = getLexer();
if (l instanceof LayeredLexer) {
editor.getDocument().putUserData(KEY, !editor.getDocument().getText().contains(PyNames.UNICODE_LITERALS));
}
super.setEditor(editor);
}
}