| /*=============================================================================# |
| # Copyright (c) 2007, 2019 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.internal.docmlet.tex.ui.editors; |
| |
| import org.eclipse.core.filesystem.EFS; |
| import org.eclipse.core.filesystem.IFileStore; |
| import org.eclipse.core.resources.IContainer; |
| import org.eclipse.core.resources.IResource; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IPath; |
| import org.eclipse.jface.text.BadLocationException; |
| |
| import org.eclipse.statet.jcommons.lang.NonNullByDefault; |
| import org.eclipse.statet.jcommons.lang.Nullable; |
| import org.eclipse.statet.jcommons.text.core.TextRegion; |
| |
| import org.eclipse.statet.docmlet.tex.core.ast.TexAst; |
| import org.eclipse.statet.docmlet.tex.core.ast.TexAstNode; |
| import org.eclipse.statet.docmlet.tex.core.commands.Argument; |
| import org.eclipse.statet.docmlet.tex.core.commands.TexCommand; |
| import org.eclipse.statet.internal.docmlet.tex.ui.sourceediting.LtxAssistInvocationContext; |
| import org.eclipse.statet.internal.docmlet.tex.ui.sourceediting.LtxAssistInvocationContext.CommandCall; |
| import org.eclipse.statet.ltk.model.core.elements.ISourceUnit; |
| import org.eclipse.statet.ltk.model.core.elements.IWorkspaceSourceUnit; |
| import org.eclipse.statet.ltk.ui.sourceediting.ISourceEditor; |
| import org.eclipse.statet.ltk.ui.sourceediting.assist.AssistInvocationContext; |
| import org.eclipse.statet.ltk.ui.sourceediting.assist.ContentAssist; |
| import org.eclipse.statet.ltk.ui.sourceediting.assist.ContentAssistComputer; |
| import org.eclipse.statet.ltk.ui.sourceediting.assist.PathCompletionComputor; |
| |
| |
| /** |
| * Completion computer for path in R code. |
| * |
| * Supports workspace properties of a R tool process |
| */ |
| @NonNullByDefault |
| public class TexPathCompletionComputer extends PathCompletionComputor { |
| |
| |
| private @Nullable IContainer baseResource; |
| private @Nullable IFileStore baseFileStore; |
| |
| |
| public TexPathCompletionComputer() { |
| } |
| |
| |
| @Override |
| public void onSessionStarted(final ISourceEditor editor, final ContentAssist assist) { |
| this.baseResource= null; |
| this.baseFileStore= null; |
| { |
| final ISourceUnit su= editor.getSourceUnit(); |
| if (su instanceof IWorkspaceSourceUnit) { |
| final IResource resource= ((IWorkspaceSourceUnit) su).getResource(); |
| if (this.baseResource == null) { |
| this.baseResource= resource.getParent(); |
| } |
| if (this.baseResource != null) { |
| try { |
| this.baseFileStore= EFS.getStore(this.baseResource.getLocationURI()); |
| } |
| catch (final CoreException e) { |
| } |
| } |
| } |
| } |
| |
| super.onSessionStarted(editor, assist); |
| } |
| |
| @Override |
| public void onSessionEnded() { |
| super.onSessionEnded(); |
| } |
| |
| @Override |
| protected char getDefaultFileSeparator() { |
| return '/'; |
| } |
| |
| @Override |
| protected @Nullable TextRegion getContentRange(final AssistInvocationContext context, final int mode) |
| throws BadLocationException { |
| if (context instanceof LtxAssistInvocationContext) { |
| final LtxAssistInvocationContext texContext= (LtxAssistInvocationContext)context; |
| final CommandCall commandCall= texContext.getCommandCall(true); |
| final int argIdx; |
| if (commandCall != null && (argIdx= commandCall.getInvocationArgIdx()) >= 0) { |
| final TexCommand command= commandCall.getCommand(); |
| final Argument argDef= command.getArguments().get(argIdx); |
| final TexAstNode argNode= commandCall.getArgNode(argIdx); |
| final int offset= texContext.getInvocationOffset(); |
| if (mode == ContentAssistComputer.SPECIFIC_MODE |
| || (argDef.getContent() & 0xf0) == Argument.RESOURCE ) { |
| final TextRegion region= TexAst.getInnerRegion(argNode); |
| if (region != null |
| && region.getStartOffset() >= offset && offset <= region.getEndOffset() ) { |
| return region; |
| } |
| } |
| } |
| } |
| return null; |
| } |
| |
| @Override |
| protected @Nullable IPath getRelativeBasePath() { |
| final IContainer baseResource= this.baseResource; |
| if (baseResource != null) { |
| return baseResource.getLocation(); |
| } |
| return null; |
| } |
| |
| @Override |
| protected @Nullable IFileStore getRelativeBaseStore() { |
| final IFileStore baseFileStore= this.baseFileStore; |
| if (baseFileStore != null) { |
| return baseFileStore; |
| } |
| return null; |
| } |
| |
| } |