Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 44d51cedefc40afe3d845883d8a20d8384eecea2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*******************************************************************************
 * Copyright (c) 2019 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.wst.html.ui.internal.contentassist;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.content.IContentTypeManager;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.ui.internal.genericeditor.ContentAssistProcessorRegistry;
import org.eclipse.ui.internal.genericeditor.GenericEditorPlugin;
import org.eclipse.wst.html.core.internal.provisional.contenttype.ContentTypeIdForHTML;
import org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext;
import org.eclipse.wst.sse.ui.contentassist.ICompletionProposalComputer;

public class GenericCompletionProposalComputer implements ICompletionProposalComputer {
	List<IContentAssistProcessor> fContentAssistProcessors = null;

	public void sessionEnded() {
		fContentAssistProcessors = null;
	}

	@Override
	public void sessionStarted() {
	}

	@Override
	public List computeCompletionProposals(CompletionProposalInvocationContext context, IProgressMonitor monitor) {
		ISourceViewer viewer = null;
		if (context.getViewer() instanceof ISourceViewer) {
			viewer = (ISourceViewer) context.getViewer();
		}
		if (fContentAssistProcessors == null && viewer != null) {
			IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
			ContentAssistProcessorRegistry contentAssistProcessorRegistry = GenericEditorPlugin.getDefault().getContentAssistProcessorRegistry();
			Set<IContentType> types = new HashSet<>();
			IContentType contentType = contentTypeManager.getContentType(ContentTypeIdForHTML.ContentTypeID_HTML);
			types.add(contentType);
			contentType = contentTypeManager.getContentType("org.eclipse.core.runtime.text");
			types.add(contentType);
			fContentAssistProcessors = contentAssistProcessorRegistry.getContentAssistProcessors(viewer, null, types);
		}
		List<ICompletionProposal> proposals = new ArrayList<>();
		for (IContentAssistProcessor processor : fContentAssistProcessors) {
			ICompletionProposal[] computed = processor.computeCompletionProposals(context.getViewer(), context.getInvocationOffset());
			proposals.addAll(Arrays.asList(computed));
		}
		return proposals;
	}

	@Override
	public List computeContextInformation(CompletionProposalInvocationContext context, IProgressMonitor monitor) {
		ISourceViewer viewer = null;
		if (context.getViewer() instanceof ISourceViewer) {
			viewer = (ISourceViewer) context.getViewer();
		}
		if (fContentAssistProcessors == null && viewer != null) {
			IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
			ContentAssistProcessorRegistry contentAssistProcessorRegistry = GenericEditorPlugin.getDefault().getContentAssistProcessorRegistry();
			Set<IContentType> types = new HashSet<>();
			IContentType contentType = contentTypeManager.getContentType(ContentTypeIdForHTML.ContentTypeID_HTML);
			types.add(contentType);
			contentType = contentTypeManager.getContentType("org.eclipse.core.runtime.text");
			types.add(contentType);
			fContentAssistProcessors = contentAssistProcessorRegistry.getContentAssistProcessors(viewer, null, types);
		}
		List<IContextInformation> infos = new ArrayList<>();
		for (IContentAssistProcessor processor : fContentAssistProcessors) {
			IContextInformation[] computed = processor.computeContextInformation(context.getViewer(), context.getInvocationOffset());
			infos.addAll(Arrays.asList(computed));
		}
		return infos;
	}

	@Override
	public String getErrorMessage() {
		// TODO Auto-generated method stub
		return null;
	}
}

Back to the top