Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autocomplete for build.properties #508

Merged
merged 1 commit into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
import org.eclipse.jdt.ui.PreferenceConstants;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.*;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.*;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.pde.internal.ui.PDEPluginImages;
import org.eclipse.pde.internal.ui.editor.PDESourcePage;
import org.eclipse.pde.internal.ui.editor.contentassist.BuildPropertiesContentAssistProcessor;
import org.eclipse.pde.internal.ui.editor.text.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
Expand All @@ -38,6 +42,9 @@ public class BuildSourceViewerConfiguration extends ChangeAwareSourceViewerConfi
private BasePDEScanner fCommentScanner;
private BasePDEScanner fPropertyValueScanner;

private ContentAssistant fContentAssistant;
private BuildPropertiesContentAssistProcessor fContentAssistantProcessor;
gireeshpunathil marked this conversation as resolved.
Show resolved Hide resolved

private abstract class AbstractJavaScanner extends BasePDEScanner {

@Override
Expand Down Expand Up @@ -231,4 +238,28 @@ public boolean affectsColorPresentation(PropertyChangeEvent event) {
String property = event.getProperty();
return property.equals(PreferenceConstants.PROPERTIES_FILE_COLORING_VALUE) || property.equals(PreferenceConstants.PROPERTIES_FILE_COLORING_ARGUMENT) || property.equals(PreferenceConstants.PROPERTIES_FILE_COLORING_ASSIGNMENT) || property.equals(PreferenceConstants.PROPERTIES_FILE_COLORING_KEY) || property.equals(PreferenceConstants.PROPERTIES_FILE_COLORING_COMMENT);
}

@Override
public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
if (fSourcePage != null && fSourcePage.isEditable()) {
if (fContentAssistant == null) {
// Initialize in SWT thread before using in background thread:
PDEPluginImages.get(null);
fContentAssistant = new ContentAssistant(true);
fContentAssistant.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
fContentAssistantProcessor = new BuildPropertiesContentAssistProcessor(fSourcePage);
fContentAssistant.setContentAssistProcessor(fContentAssistantProcessor, IDocument.DEFAULT_CONTENT_TYPE);
fContentAssistant.setContentAssistProcessor(fContentAssistantProcessor, PROPERTY_VALUE);
fContentAssistant.addCompletionListener(fContentAssistantProcessor);
fContentAssistant.enableAutoInsert(true);
fContentAssistant.setInformationControlCreator(parent -> new DefaultInformationControl(parent, false));
fContentAssistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
fContentAssistant.enableAutoActivation(true);
}
return fContentAssistant;
}
return null;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*******************************************************************************
* Copyright (c) 2023 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.pde.internal.ui.editor.contentassist;

import java.lang.reflect.Field;
import java.util.ArrayList;
import org.eclipse.jface.text.*;
import org.eclipse.jface.text.contentassist.*;
import org.eclipse.pde.internal.build.IBuildPropertiesConstants;
import org.eclipse.pde.internal.ui.editor.PDESourcePage;

public class BuildPropertiesContentAssistProcessor extends TypePackageCompletionProcessor
implements ICompletionListener {
gireeshpunathil marked this conversation as resolved.
Show resolved Hide resolved

protected PDESourcePage fSourcePage;
public BuildPropertiesContentAssistProcessor(PDESourcePage sourcePage) {
fSourcePage = sourcePage;
}

@Override
public void assistSessionStarted(ContentAssistEvent event) {
// TODO Auto-generated method stub

}

@Override
public void assistSessionEnded(ContentAssistEvent event) {
// TODO Auto-generated method stub

}

@Override
public void selectionChanged(ICompletionProposal proposal, boolean smartToggle) {
// TODO Auto-generated method stub

}

@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
IDocument doc = fSourcePage.getDocumentProvider().getDocument(fSourcePage.getInputContext().getInput());
try {
int lineNum = doc.getLineOfOffset(offset);
int lineStart = doc.getLineOffset(lineNum);
String value = doc.get(lineStart, offset - lineStart);
ArrayList<TypeCompletionProposal> completions = new ArrayList<>();
Field[] properties = IBuildPropertiesConstants.class.getFields();
for (Field f : properties) {
String key = f.getName();
String element = "";
gireeshpunathil marked this conversation as resolved.
Show resolved Hide resolved
try {
element = (String) f.get(key);
} catch (IllegalAccessException e) {
continue;
}
if (element.regionMatches(true, 0, value, 0, value.length())) {
TypeCompletionProposal proposal = new TypeCompletionProposal(element, null, element, lineStart,
value.length());
completions.add(proposal);
}
}
return completions.toArray(new ICompletionProposal[completions.size()]);
gireeshpunathil marked this conversation as resolved.
Show resolved Hide resolved
} catch (BadLocationException e) {
}
return null;
}
}