blob: 030ae00e25b331c662e0aaba0758a31bc4065749 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2012, 2018 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.docmlet.wikitext.ui.actions;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.text.IRegion;
import org.eclipse.statet.docmlet.wikitext.core.ast.WikitextAst;
import org.eclipse.statet.docmlet.wikitext.core.ast.WikitextAstNode;
import org.eclipse.statet.docmlet.wikitext.core.model.IWikidocModelInfo;
import org.eclipse.statet.docmlet.wikitext.core.model.IWikitextSourceUnit;
import org.eclipse.statet.docmlet.wikitext.core.model.WikitextModel;
import org.eclipse.statet.docmlet.wikitext.core.model.WikitextNameAccess;
import org.eclipse.statet.ltk.ast.core.AstInfo;
import org.eclipse.statet.ltk.ast.core.AstNode;
import org.eclipse.statet.ltk.ast.core.util.AstSelection;
import org.eclipse.statet.ltk.model.core.IModelManager;
import org.eclipse.statet.ltk.model.core.elements.ISourceUnit;
import org.eclipse.statet.ltk.ui.sourceediting.ISourceEditor;
import org.eclipse.statet.ltk.ui.sourceediting.actions.AbstractOpenDeclarationHandler;
import org.eclipse.statet.ltk.ui.sourceediting.actions.OpenDeclaration;
public class WikidocOpenDeclarationHandler extends AbstractOpenDeclarationHandler {
// private static final class WikitextOpenDeclaration extends OpenDeclaration {
//
// @Override
// public ILabelProvider createLabelProvider() {
// return new WikitextLabelProvider();
// }
// }
public static WikitextNameAccess searchAccess(final ISourceUnit sourceUnit, final IRegion region) {
if (sourceUnit instanceof IWikitextSourceUnit) {
final IWikidocModelInfo info= (IWikidocModelInfo) sourceUnit.getModelInfo(
WikitextModel.WIKIDOC_TYPE_ID, IModelManager.MODEL_FILE, new NullProgressMonitor());
if (info != null) {
final AstInfo astInfo= info.getAst();
final AstSelection selection= AstSelection.search(astInfo.getRoot(),
region.getOffset(), region.getOffset()+region.getLength(),
AstSelection.MODE_COVERING_SAME_LAST );
final AstNode covering= selection.getCovering();
if (covering instanceof WikitextAstNode) {
final WikitextAstNode node= (WikitextAstNode) covering;
if (node.getNodeType() == WikitextAst.NodeType.LABEL) {
WikitextAstNode current= node;
do {
for (final Object attachment : current.getAttachments()) {
if (attachment instanceof WikitextNameAccess) {
final WikitextNameAccess access= (WikitextNameAccess) attachment;
if (access.getNameNode() == node) {
return access;
}
}
}
current= current.getWikitextParent();
} while (current != null);
}
}
}
}
return null;
}
@Override
public boolean execute(final ISourceEditor editor, final IRegion selection) {
final WikitextNameAccess access= searchAccess(editor.getSourceUnit(), selection);
if (access != null) {
final OpenDeclaration open= new OpenDeclaration();
final WikitextNameAccess declAccess= open.selectAccess(access.getAllInUnit());
if (declAccess != null) {
open.open(editor, declAccess);
return true;
}
}
return false;
}
}