Java Code Examples for org.eclipse.ui.texteditor.IDocumentProvider#connect()

The following examples show how to use org.eclipse.ui.texteditor.IDocumentProvider#connect() . 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: TLAHyperlinkDetector.java    From tlaplus with MIT License 6 votes vote down vote up
private int[] getDocumentAndRegion(SyntaxTreeNode csNode, IDocumentProvider documentProvider, IEditorInput editorInput)
		throws CoreException {
	IDocument document;
	// connect to the resource
	try {
		documentProvider.connect(editorInput);
		document = documentProvider.getDocument(editorInput);
	} finally {
		/*
		 * Once the document has been retrieved, the document provider is not needed.
		 * Always disconnect it to avoid a memory leak.
		 * 
		 * Keeping it connected only seems to provide synchronization of the document
		 * with file changes. That is not necessary in this context.
		 */
		documentProvider.disconnect(editorInput);
	}

	return getRegion(csNode, document);
}
 
Example 2
Source File: EditorUtils.java    From typescript.java with MIT License 6 votes vote down vote up
public static Position getPosition(IFile file, TextSpan textSpan) throws BadLocationException {
	ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
	ITextFileBuffer buffer = bufferManager.getTextFileBuffer(file.getLocation(), LocationKind.IFILE);
	if (buffer != null) {
		return getPosition(buffer.getDocument(), textSpan);
	}
	IDocumentProvider provider = new TextFileDocumentProvider();
	try {
		provider.connect(file);
		IDocument document = provider.getDocument(file);
		if (document != null) {
			return getPosition(document, textSpan);
		}
	} catch (CoreException e) {
	} finally {
		provider.disconnect(file);
	}
	return null;
}
 
Example 3
Source File: N4JSStackTraceHyperlink.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private void revealLocationInFile(String typeName, int line, int column, IEditorPart editorPart)
		throws CoreException {
	if (editorPart instanceof ITextEditor && line >= 0) {
		ITextEditor textEditor = (ITextEditor) editorPart;
		IDocumentProvider provider = textEditor.getDocumentProvider();
		IEditorInput editorInput = editorPart.getEditorInput();
		provider.connect(editorInput);
		IDocument document = provider.getDocument(editorInput);
		try {
			IRegion regionOfLine = document.getLineInformation(line);
			// only used to reveal the location
			textEditor.selectAndReveal(regionOfLine.getOffset(), regionOfLine.getLength());
			int startOffset = regionOfLine.getOffset() + column;
			int length = regionOfLine.getLength() - column;
			if (startOffset >= document.getLength()) {
				startOffset = document.getLength() - 1;
			}
			if (length + startOffset >= document.getLength()) {
				length = document.getLength() - startOffset - 1;
			}
			textEditor.setHighlightRange(startOffset, length, true);
		} catch (BadLocationException e) {
			MessageDialog.openInformation(N4JSGracefulActivator.getActiveWorkbenchShell(),
					ConsoleMessages.msgInvalidLineNumberTitle(),
					ConsoleMessages.msgInvalidLineNumberIn(
							(line + 1) + "",
							typeName));
		}
		provider.disconnect(editorInput);
	}
}
 
Example 4
Source File: TestResultHyperlink.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private void revealLocationInFile(IEditorPart editorPart)
		throws CoreException {
	if (editorPart instanceof ITextEditor && locationText.line > 0) {
		ITextEditor textEditor = (ITextEditor) editorPart;
		IDocumentProvider provider = textEditor.getDocumentProvider();
		IEditorInput editorInput = editorPart.getEditorInput();
		provider.connect(editorInput);
		IDocument document = provider.getDocument(editorInput);
		try {
			IRegion regionOfLine = document.getLineInformation(locationText.line - 1);
			// only used to reveal the location
			textEditor.selectAndReveal(regionOfLine.getOffset(), regionOfLine.getLength());
			int startOffset = regionOfLine.getOffset() + locationText.column - 1;
			int length = regionOfLine.getLength() - locationText.column;
			if (startOffset >= document.getLength()) {
				startOffset = document.getLength() - 1;
			}
			if (length + startOffset >= document.getLength()) {
				length = document.getLength() - startOffset - 1;
			}
			textEditor.setHighlightRange(startOffset, length, true);
		} catch (BadLocationException e) {
			MessageDialog.openInformation(N4JSGracefulActivator.getActiveWorkbenchShell(),
					ConsoleMessages.msgInvalidLineNumberTitle(),
					ConsoleMessages.msgInvalidLineNumberIn(
							(locationText.line) + "",
							locationText.fileName));
		}
		provider.disconnect(editorInput);
	}
}
 
Example 5
Source File: JavaStatusContextViewer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IDocument getDocument(IDocumentProvider provider, IEditorInput input) {
	if (input == null)
		return null;
	IDocument result= null;
	try {
		provider.connect(input);
		result= provider.getDocument(input);
	} catch (CoreException e) {
	} finally {
		provider.disconnect(input);
	}
	return result;
}
 
Example 6
Source File: TestEditor.java    From ermasterr with Apache License 2.0 3 votes vote down vote up
protected void doSetInput(final IEditorInput input) throws CoreException {
    if (input == null) {
        close(isSaveOnCloseNeeded());

    } else {
        final IEditorInput oldInput = getEditorInput();
        if (oldInput != null)
            getDocumentProvider().disconnect(oldInput);

        super.setInput(input);

        updateDocumentProvider(input);

        final IDocumentProvider provider = getDocumentProvider();

        provider.connect(input);

        initializeTitle(input);

        if (fSourceViewer != null) {
            initializeSourceViewer(input);

        }

    }

}
 
Example 7
Source File: EditorAPI.java    From saros with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Can be called instead of {@link #getDocumentProvider(IEditorInput)} and {@link
 * IDocumentProvider#connect(Object) provider.connect()} in a try-catch block. This method logs an
 * error in case of an Exception and returns <code>null</code>. Otherwise, it returns the
 * connected provider.
 *
 * <p>Example usage:
 *
 * <pre>
 * <code>
 * IDocumentProvider provider = EditorAPI.connect(editorInput)
 *
 * if (provider == null) {
 *  doErrorHandling
 *  return;
 * }
 *
 * try {
 *  doLogic
 * }
 * finally {
 *  provider.disconnect(editorInput);
 * }
 * </code>
 * </pre>
 */
public static IDocumentProvider connect(IEditorInput input) {
  IDocumentProvider provider = getDocumentProvider(input);
  try {
    provider.connect(input);
  } catch (CoreException e) {
    log.error("could not connect to document provider for file: " + input.getName(), e);
    return null;
  }

  return provider;
}
 
Example 8
Source File: TestEditor.java    From ermaster-b with Apache License 2.0 3 votes vote down vote up
protected void doSetInput(IEditorInput input) throws CoreException {
	if (input == null) {
		close(isSaveOnCloseNeeded());

	} else {
		IEditorInput oldInput = getEditorInput();
		if (oldInput != null)
			getDocumentProvider().disconnect(oldInput);

		super.setInput(input);

		updateDocumentProvider(input);

		IDocumentProvider provider = getDocumentProvider();

		provider.connect(input);

		initializeTitle(input);

		if (fSourceViewer != null) {
			initializeSourceViewer(input);

		}

	}

}