/platform/platform-impl/src/com/intellij/openapi/editor/impl/EditorHighlighterCache.java

https://bitbucket.org/nbargnesi/idea · Java · 57 lines · 31 code · 8 blank · 18 comment · 4 complexity · b6b28b02d1480f56332a1b261ff6f6ec MD5 · raw file

  1. /*
  2. * Copyright 2000-2011 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.openapi.editor.impl;
  17. import com.intellij.openapi.editor.Document;
  18. import com.intellij.openapi.editor.ex.util.LexerEditorHighlighter;
  19. import com.intellij.openapi.editor.highlighter.EditorHighlighter;
  20. import com.intellij.openapi.util.Key;
  21. import org.jetbrains.annotations.NotNull;
  22. import org.jetbrains.annotations.Nullable;
  23. import java.lang.ref.WeakReference;
  24. /**
  25. * @author yole
  26. */
  27. public class EditorHighlighterCache {
  28. private static final Key<WeakReference<EditorHighlighter>> ourSomeEditorSyntaxHighlighter = Key.create("some editor highlighter");
  29. private EditorHighlighterCache() {
  30. }
  31. public static void rememberEditorHighlighterForCachesOptimization(Document document, @NotNull final EditorHighlighter highlighter) {
  32. document.putUserData(ourSomeEditorSyntaxHighlighter, new WeakReference<EditorHighlighter>(highlighter));
  33. }
  34. @Nullable
  35. public static EditorHighlighter getEditorHighlighterForCachesBuilding(Document document) {
  36. if (document == null) {
  37. return null;
  38. }
  39. final WeakReference<EditorHighlighter> editorHighlighterWeakReference = document.getUserData(ourSomeEditorSyntaxHighlighter);
  40. final EditorHighlighter someEditorHighlighter = editorHighlighterWeakReference != null ? editorHighlighterWeakReference.get() : null;
  41. if (someEditorHighlighter instanceof LexerEditorHighlighter &&
  42. ((LexerEditorHighlighter)someEditorHighlighter).isValid()
  43. ) {
  44. return someEditorHighlighter;
  45. }
  46. document.putUserData(ourSomeEditorSyntaxHighlighter, null);
  47. return null;
  48. }
  49. }