com.intellij.openapi.fileEditor.FileEditorState Java Examples

The following examples show how to use com.intellij.openapi.fileEditor.FileEditorState. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HistoryEntry.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * @return element that was added to the <code>element</code>.
 * Returned element has tag {@link #TAG}. Never null.
 */
public Element writeExternal(Element element, Project project) {
  Element e = new Element(TAG);
  element.addContent(e);
  e.setAttribute(FILE_ATTR, myFilePointer.getUrl());

  for (final Map.Entry<FileEditorProvider, FileEditorState> entry : myProvider2State.entrySet()) {
    FileEditorProvider provider = entry.getKey();

    Element providerElement = new Element(PROVIDER_ELEMENT);
    if (provider.equals(mySelectedProvider)) {
      providerElement.setAttribute(SELECTED_ATTR_VALUE, Boolean.TRUE.toString());
    }
    providerElement.setAttribute(EDITOR_TYPE_ID_ATTR, provider.getEditorTypeId());
    Element stateElement = new Element(STATE_ELEMENT);
    providerElement.addContent(stateElement);
    provider.writeState(entry.getValue(), project, stateElement);

    e.addContent(providerElement);
  }

  return e;
}
 
Example #2
Source File: DesktopPsiAwareTextEditorProvider.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@Nonnull
public FileEditorState readState(@Nonnull final Element element, @Nonnull final Project project, @Nonnull final VirtualFile file) {
  final TextEditorState state = (TextEditorState)super.readState(element, project, file);

  // Foldings
  Element child = element.getChild(FOLDING_ELEMENT);
  Document document = FileDocumentManager.getInstance().getCachedDocument(file);
  if (child != null) {
    if (document == null) {
      final Element detachedStateCopy = child.clone();
      state.setDelayedFoldState(() -> {
        Document document1 = FileDocumentManager.getInstance().getCachedDocument(file);
        return document1 == null ? null : CodeFoldingManager.getInstance(project).readFoldingState(detachedStateCopy, document1);
      });
    }
    else {
      //PsiDocumentManager.getInstance(project).commitDocument(document);
      state.setFoldingState(CodeFoldingManager.getInstance(project).readFoldingState(child, document));
    }
  }
  return state;
}
 
Example #3
Source File: HistoryEntry.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static HistoryEntry createHeavy(@Nonnull Project project,
                                       @Nonnull VirtualFile file,
                                       @Nonnull FileEditorProvider[] providers,
                                       @Nonnull FileEditorState[] states,
                                       @Nonnull FileEditorProvider selectedProvider) {
  if (project.isDisposed()) return createLight(file, providers, states, selectedProvider);

  Disposable disposable = Disposable.newDisposable();
  VirtualFilePointer pointer = VirtualFilePointerManager.getInstance().create(file, disposable, null);

  HistoryEntry entry = new HistoryEntry(pointer, selectedProvider, disposable);
  for (int i = 0; i < providers.length; i++) {
    FileEditorProvider provider = providers[i];
    FileEditorState state = states[i];
    if (provider != null && state != null) {
      entry.putState(provider, state);
    }
  }
  return entry;
}
 
Example #4
Source File: DesktopPsiAwareTextEditorProvider.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void writeState(@Nonnull final FileEditorState _state, @Nonnull final Project project, @Nonnull final Element element) {
  super.writeState(_state, project, element);

  TextEditorState state = (TextEditorState)_state;

  // Foldings
  CodeFoldingState foldingState = state.getFoldingState();
  if (foldingState != null) {
    Element e = new Element(FOLDING_ELEMENT);
    try {
      CodeFoldingManager.getInstance(project).writeFoldingState(foldingState, e);
    }
    catch (WriteExternalException e1) {
      //ignore
    }
    element.addContent(e);
  }
}
 
Example #5
Source File: CsvTableEditorProviderTest.java    From intellij-csv-validator with Apache License 2.0 6 votes vote down vote up
public void testWriteAndReadTableEditorState() {
    FileEditorProvider[] fileEditorProviders = FileEditorProviderManager.getInstance().getProviders(getProject(), myFixture.getFile().getVirtualFile());
    FileEditorProvider fileEditorProvider = fileEditorProviders[1];

    CsvTableEditorState editorState = new CsvTableEditorState();
    editorState.setColumnWidths(new int[]{ 120, 32, 9});
    editorState.setRowLines(5);
    editorState.setShowInfoPanel(false);

    Element element = new Element("state");
    fileEditorProvider.writeState(editorState, getProject(), element);

    FileEditorState readState = fileEditorProvider.readState(element, getProject(), myFixture.getFile().getVirtualFile());

    assertInstanceOf(readState, CsvTableEditorState.class);

    CsvTableEditorState editorStateRead = (CsvTableEditorState)readState;
    assertTrue(Objects.deepEquals(editorState.getColumnWidths(), editorStateRead.getColumnWidths()));
    assertEquals(editorState.getRowLines(), editorStateRead.getRowLines());
    assertEquals(editorState.showInfoPanel(), editorStateRead.showInfoPanel());
}
 
Example #6
Source File: TextEditorState.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean canBeMergedWith(FileEditorState otherState, FileEditorStateLevel level) {
  if (!(otherState instanceof TextEditorState)) return false;
  TextEditorState other = (TextEditorState)otherState;
  return level == FileEditorStateLevel.NAVIGATION
         && CARETS != null && CARETS.length == 1
         && other.CARETS != null && other.CARETS.length == 1
         && Math.abs(CARETS[0].LINE - other.CARETS[0].LINE) < MIN_CHANGE_DISTANCE;
}
 
Example #7
Source File: HistoryEntry.java    From consulo with Apache License 2.0 5 votes vote down vote up
public EntryData(@Nonnull String url,
                 @Nonnull List<Pair<FileEditorProvider, FileEditorState>> providerStates,
                 @Nullable FileEditorProvider selectedProvider) {
  this.url = url;
  this.providerStates = providerStates;
  this.selectedProvider = selectedProvider;
}
 
Example #8
Source File: HistoryEntry.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static EntryData parseEntry(@Nonnull Project project, @Nonnull Element e) throws InvalidDataException {
  if (!e.getName().equals(TAG)) {
    throw new IllegalArgumentException("unexpected tag: " + e);
  }

  String url = e.getAttributeValue(FILE_ATTR);
  List<Pair<FileEditorProvider, FileEditorState>> providerStates = new ArrayList<>();
  FileEditorProvider selectedProvider = null;

  VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(url);

  for (Element _e : e.getChildren(PROVIDER_ELEMENT)) {
    String typeId = _e.getAttributeValue(EDITOR_TYPE_ID_ATTR);
    FileEditorProvider provider = FileEditorProviderManager.getInstance().getProvider(typeId);
    if (provider == null) {
      continue;
    }
    if (Boolean.valueOf(_e.getAttributeValue(SELECTED_ATTR_VALUE))) {
      selectedProvider = provider;
    }

    Element stateElement = _e.getChild(STATE_ELEMENT);
    if (stateElement == null) {
      throw new InvalidDataException();
    }

    if (file != null) {
      FileEditorState state = provider.readState(stateElement, project, file);
      providerStates.add(Pair.create(provider, state));
    }
  }

  return new EntryData(url, providerStates, selectedProvider);
}
 
Example #9
Source File: CsvTableEditorProvider.java    From intellij-csv-validator with Apache License 2.0 5 votes vote down vote up
@Override
public void writeState(@NotNull FileEditorState state, @NotNull Project project, @NotNull Element targetElement) {
    if (!(state instanceof CsvTableEditorState)) {
        return;
    }
    CsvTableEditorState csvTableEditorState = (CsvTableEditorState) state;
    csvTableEditorState.write(project, targetElement);
}
 
Example #10
Source File: DesktopEditorWithProviderComposite.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public HistoryEntry currentStateAsHistoryEntry() {
  final FileEditor[] editors = getEditors();
  final FileEditorState[] states = new FileEditorState[editors.length];
  for (int j = 0; j < states.length; j++) {
    states[j] = editors[j].getState(FileEditorStateLevel.FULL);
    LOG.assertTrue(states[j] != null);
  }
  final int selectedProviderIndex = ArrayUtil.find(editors, getSelectedEditor());
  LOG.assertTrue(selectedProviderIndex != -1);
  final FileEditorProvider[] providers = getProviders();
  return HistoryEntry.createLight(getFile(), providers, states, providers[selectedProviderIndex]);
}
 
Example #11
Source File: HistoryEntry.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static HistoryEntry createHeavy(@Nonnull Project project, @Nonnull Element e) throws InvalidDataException {
  if (project.isDisposed()) return createLight(project, e);

  EntryData entryData = parseEntry(project, e);

  Disposable disposable = Disposable.newDisposable();
  VirtualFilePointer pointer = VirtualFilePointerManager.getInstance().create(entryData.url, disposable, null);

  HistoryEntry entry = new HistoryEntry(pointer, entryData.selectedProvider, disposable);
  for (Pair<FileEditorProvider, FileEditorState> state : entryData.providerStates) {
    entry.putState(state.first, state.second);
  }
  return entry;
}
 
Example #12
Source File: UndoRedo.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean restore(EditorAndState pair, boolean onlyIfDiffers) {
  if (myEditor == null ||
      !myEditor.isValid() || // editor can be invalid if underlying file is deleted during undo (e.g. after undoing scratch file creation)
      pair == null || pair.getEditor() == null) {
    return false;
  }

  // we cannot simply compare editors here because of the following scenario:
  // 1. make changes in editor for file A
  // 2. move caret
  // 3. close editor
  // 4. re-open editor for A via Ctrl-E
  // 5. undo -> position is not affected, because instance created in step 4 is not the same!!!
  if (!myEditor.getClass().equals(pair.getEditor().getClass())) {
    return false;
  }

  // If current editor state isn't equals to remembered state then
  // we have to try to restore previous state. But sometime it's
  // not possible to restore it. For example, it's not possible to
  // restore scroll proportion if editor doesn not have scrolling any more.
  FileEditorState currentState = myEditor.getState(FileEditorStateLevel.UNDO);
  if (onlyIfDiffers && currentState.equals(pair.getState())) {
    return false;
  }

  myEditor.setState(pair.getState());
  return true;
}
 
Example #13
Source File: HistoryEntry.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static HistoryEntry createLight(@Nonnull Project project, @Nonnull Element e) throws InvalidDataException {
  EntryData entryData = parseEntry(project, e);

  VirtualFilePointer pointer = new LightFilePointer(entryData.url);
  HistoryEntry entry = new HistoryEntry(pointer, entryData.selectedProvider, null);
  for (Pair<FileEditorProvider, FileEditorState> state : entryData.providerStates) {
    entry.putState(state.first, state.second);
  }
  return entry;
}
 
Example #14
Source File: HistoryEntry.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static HistoryEntry createLight(@Nonnull VirtualFile file,
                                       @Nonnull FileEditorProvider[] providers,
                                       @Nonnull FileEditorState[] states,
                                       @Nonnull FileEditorProvider selectedProvider) {
  VirtualFilePointer pointer = new LightFilePointer(file);
  HistoryEntry entry = new HistoryEntry(pointer, selectedProvider, null);
  for (int i = 0; i < providers.length; i++) {
    entry.putState(providers[i], states[i]);
  }
  return entry;
}
 
Example #15
Source File: MyFileEditorProvider.java    From MavenHelper with Apache License 2.0 4 votes vote down vote up
@NotNull
public FileEditorState readState(@NotNull final Element element, @NotNull final Project project,
		@NotNull final VirtualFile file) {
	return UIFormEditor.MY_EDITOR_STATE;
}
 
Example #16
Source File: MyFileEditorProvider.java    From MavenHelper with Apache License 2.0 4 votes vote down vote up
public void writeState(@NotNull final FileEditorState state, @NotNull final Project project,
		@NotNull final Element element) {
}
 
Example #17
Source File: HistoryEntry.java    From consulo with Apache License 2.0 4 votes vote down vote up
public FileEditorState getState(@Nonnull FileEditorProvider provider) {
  return myProvider2State.get(provider);
}
 
Example #18
Source File: TransferableFileEditorStateSupport.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
private static TransferableFileEditorState getEditorState(@Nonnull FileEditor editor) {
  FileEditorState state = editor.getState(FileEditorStateLevel.FULL);
  return state instanceof TransferableFileEditorState ? (TransferableFileEditorState)state : null;
}
 
Example #19
Source File: HttpFileEditorProvider.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public FileEditorState readState(@Nonnull final Element sourceElement, @Nonnull final Project project, @Nonnull final VirtualFile file) {
  return new TextEditorState();
}
 
Example #20
Source File: UIFormEditor.java    From MavenHelper with Apache License 2.0 4 votes vote down vote up
public void setState(@NotNull final FileEditorState state) {
}
 
Example #21
Source File: HttpFileEditorProvider.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void writeState(@Nonnull final FileEditorState state, @Nonnull final Project project, @Nonnull final Element targetElement) {
}
 
Example #22
Source File: CSharpAssemblyFileEditorProvider.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Override
public void writeState(@Nonnull FileEditorState fileEditorState, @Nonnull Project project, @Nonnull Element element)
{

}
 
Example #23
Source File: HistoryEntry.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void putState(@Nonnull FileEditorProvider provider, @Nonnull FileEditorState state) {
  myProvider2State.put(provider, state);
}
 
Example #24
Source File: EditorAndState.java    From consulo with Apache License 2.0 4 votes vote down vote up
public EditorAndState(FileEditor editor, FileEditorState state) {
  myEditor = new WeakReference<FileEditor>(editor);
  myState = state;
}
 
Example #25
Source File: EditorAndState.java    From consulo with Apache License 2.0 4 votes vote down vote up
public FileEditorState getState() {
  return myState;
}
 
Example #26
Source File: WebTextEditor.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public FileEditorState getState(@Nonnull FileEditorStateLevel level) {
  return null;
}
 
Example #27
Source File: MindMapDocumentEditor.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void setState(@Nonnull FileEditorState fileEditorState) {
}
 
Example #28
Source File: CmtFileEditor.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
@Override
public void setState(@NotNull FileEditorState state) {
}
 
Example #29
Source File: CsvTableEditorProvider.java    From intellij-csv-validator with Apache License 2.0 4 votes vote down vote up
@Override
public FileEditorState readState(@NotNull Element sourceElement, @NotNull Project project, @NotNull VirtualFile file) {
    return CsvTableEditorState.create(sourceElement, project, file);
}
 
Example #30
Source File: CsvTableEditorState.java    From intellij-csv-validator with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canBeMergedWith(FileEditorState fileEditorState, FileEditorStateLevel fileEditorStateLevel) {
    return false;
}