How to access getViewer().doOperation(TextViewer.PREFIX) an TextEditor? [message #1853950] |
Mon, 25 July 2022 09:10  |
Eclipse User |
|
|
|
Hi,
I would like to rewrite functionality for a particural org.eclipse.core.commands.AbstractHandler which was coded with an own editor in mind. I would like that it works as well with the ExtensionBasedTextEditor.
The previous custom editor made the method getSourceViewer public an then the implementation of the handler was just
final TextEditor activeEditor = getActiveEditor().get();
final ISelection sourceSelection = activeEditor.getSelectionProvider().getSelection();
if (sourceSelection instanceof ITextSelection) {
if (isSelectionCommented((ITextSelection) sourceSelection)) {
activeEditor.getSourceViewer().doOperation(TextViewer.STRIP_PREFIX);
} else {
activeEditor.getSourceViewer().doOperation(TextViewer.PREFIX);
}
}
since the getSourceViewer() is protected in the AbstractTextEditor and TextEditor does not make it public, I cannot do this call for a generic TextEditor. I get the error The method getSourceViewer() from the type AbstractTextEditor is not visible.
Any ideas how can I reimplement my logic for a TextEditor?
Thanks
|
|
|
Re: How to access getViewer().doOperation(TextViewer.PREFIX) an TextEditor? [message #1853951 is a reply to message #1853950] |
Mon, 25 July 2022 09:18  |
Eclipse User |
|
|
|
If it helps, this is the class I want to write
/** The prefixes. */
private Map<String, String[]> prefixesMap = null;
/**
* Check if ALL of the lines in the given <code>selection</code> are commented.
*
* @param selection
* the <code>ITextSelection</code> to check
* @return true if all lines in the given <code>selection</code> are commented, else false
* @throws IllegalStateException
* if the line number is invalid
*/
private boolean isSelectionCommented(final ITextSelection selection) throws IllegalStateException {
final int startLine = selection.getStartLine();
final int endLine = selection.getEndLine();
for (int i = startLine; i <= endLine; i++) {
try {
final IDocument document = getActiveEditor().get().getDocumentProvider().getDocument(null);
final IRegion region = document.getLineInformation(i);
String line = document.get(region.getOffset(), region.getLength());
line = StringUtils.stripToEmpty(line);
final Collection<String[]> c = prefixesMap.values();
boolean isComment = false;
for (final String[] a : c) {
for (final String s : a) {
if (line.startsWith(s)) {
// line i is commented
isComment = true;
}
}
}
if (!isComment) {
return false;
}
} catch (final BadLocationException e) {
throw new IllegalStateException(e);
}
}
return true;
}
/**
* Evaluate the prefixes used for commenting a line and store it in this {@link #prefixesMap}.
*/
private void setPrefixMap() {
if (prefixesMap != null) {
return;
}
final TextEditor activeEditor = getActiveEditor().get();
final String[] types = activeEditor.getSourceViewerConfiguration().getConfiguredContentTypes(
activeEditor.getSourceViewer());
final Map<String, String[]> pMap = new HashMap<>(types.length);
for (final String type : types) {
String[] prefixes = activeEditor.getSourceViewerConfiguration().getDefaultPrefixes(
activeEditor.getSourceViewer(), type);
if (prefixes != null && prefixes.length > 0) {
int emptyPrefixes = 0;
for (final String prefixe : prefixes) {
if (prefixe.length() == 0) {
emptyPrefixes++;
}
}
if (emptyPrefixes > 0) {
final String[] nonemptyPrefixes = new String[prefixes.length - emptyPrefixes];
for (int j = 0, k = 0; j < prefixes.length; j++) {
final String prefix = prefixes[j];
if (prefix.length() != 0) {
nonemptyPrefixes[k] = prefix;
k++;
}
}
prefixes = nonemptyPrefixes;
}
pMap.put(type, prefixes);
}
}
prefixesMap = pMap;
}
/** {@inheritDoc} */
@Override
protected void execute(final ISelection selection) throws ExecutionException {
// evaluate the prefixes used for comments
setPrefixMap();
final TextEditor activeEditor = getActiveEditor().get();
final ISelection sourceSelection = activeEditor.getSelectionProvider().getSelection();
if (sourceSelection instanceof ITextSelection) {
if (isSelectionCommented((ITextSelection) sourceSelection)) {
activeEditor.getSourceViewer().doOperation(TextViewer.STRIP_PREFIX);
} else {
activeEditor.getSourceViewer().doOperation(TextViewer.PREFIX);
}
}
}
My problem is that getSourceViewer() and getSourceViewerConfiguration() are protected.
|
|
|
Powered by
FUDForum. Page generated in 0.43057 seconds