/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/text/TextEditorState.java

https://bitbucket.org/nbargnesi/idea · Java · 110 lines · 63 code · 15 blank · 32 comment · 23 complexity · ef9d9789adbeefb9f24fd9bfebb6e072 MD5 · raw file

  1. /*
  2. * Copyright 2000-2012 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.fileEditor.impl.text;
  17. import com.intellij.openapi.editor.Document;
  18. import com.intellij.openapi.fileEditor.FileEditorState;
  19. import com.intellij.openapi.fileEditor.FileEditorStateLevel;
  20. import com.intellij.util.Producer;
  21. import org.jetbrains.annotations.NotNull;
  22. import org.jetbrains.annotations.Nullable;
  23. /**
  24. * @author Vladimir Kondratyev
  25. */
  26. public final class TextEditorState implements FileEditorState {
  27. public int LINE;
  28. public int COLUMN;
  29. public float VERTICAL_SCROLL_PROPORTION;
  30. public int SELECTION_START;
  31. public int SELECTION_END;
  32. /**
  33. * State which describes how editor is folded.
  34. * This field can be <code>null</code>.
  35. */
  36. private CodeFoldingState myFoldingState;
  37. @Nullable private Producer<CodeFoldingState> myDelayedFoldInfoProducer;
  38. private static final int MIN_CHANGE_DISTANCE = 4;
  39. public TextEditorState() {
  40. }
  41. /**
  42. * Folding state is more complex than, say, line/column number, that's why it's deserialization can be performed only when
  43. * necessary pre-requisites are met (e.g. corresponding {@link Document} is created).
  44. * <p/>
  45. * However, we can't be sure that those conditions are met on IDE startup (when editor states are read). Current method allows
  46. * to register a closure within the current state object which returns folding info if possible.
  47. *
  48. * @param producer delayed folding info producer
  49. */
  50. public void setDelayedFoldState(@NotNull Producer<CodeFoldingState> producer) {
  51. myDelayedFoldInfoProducer = producer;
  52. }
  53. @Nullable
  54. public CodeFoldingState getFoldingState() {
  55. // Assuming single-thread access here.
  56. if (myFoldingState == null && myDelayedFoldInfoProducer != null) {
  57. myFoldingState = myDelayedFoldInfoProducer.produce();
  58. if (myFoldingState != null) {
  59. myDelayedFoldInfoProducer = null;
  60. }
  61. }
  62. return myFoldingState;
  63. }
  64. public void setFoldingState(@Nullable CodeFoldingState foldingState) {
  65. myFoldingState = foldingState;
  66. myDelayedFoldInfoProducer = null;
  67. }
  68. public boolean equals(Object o) {
  69. if (!(o instanceof TextEditorState)) {
  70. return false;
  71. }
  72. final TextEditorState textEditorState = (TextEditorState)o;
  73. if (COLUMN != textEditorState.COLUMN) return false;
  74. if (LINE != textEditorState.LINE) return false;
  75. if (VERTICAL_SCROLL_PROPORTION != textEditorState.VERTICAL_SCROLL_PROPORTION) return false;
  76. if (SELECTION_START != textEditorState.SELECTION_START) return false;
  77. if (SELECTION_END != textEditorState.SELECTION_END) return false;
  78. CodeFoldingState localFoldingState = getFoldingState();
  79. CodeFoldingState theirFoldingState = textEditorState.getFoldingState();
  80. if (localFoldingState == null ? theirFoldingState != null : !localFoldingState.equals(theirFoldingState)) return false;
  81. return true;
  82. }
  83. public int hashCode() {
  84. return LINE + COLUMN;
  85. }
  86. @Override
  87. public boolean canBeMergedWith(FileEditorState otherState, FileEditorStateLevel level) {
  88. if (!(otherState instanceof TextEditorState)) return false;
  89. TextEditorState other = (TextEditorState)otherState;
  90. return level == FileEditorStateLevel.NAVIGATION && Math.abs(LINE - other.LINE) < MIN_CHANGE_DISTANCE;
  91. }
  92. public String toString() {
  93. return "[" + LINE + "," + COLUMN + "]";
  94. }
  95. }