Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c4a82ba1a2d0c2b23de641d61326f8eea11f1863 (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
/**
 *  Copyright (c) 2017 Angelo ZERR.
 *
 *  This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *  Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Add CodeMining support in SourceViewer - Bug 527515
 */
package org.eclipse.jface.text.examples.codemining;

import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.codemining.ICodeMiningProvider;

/**
 * Class reference mining.
 *
 */
public class ClassReferenceCodeMining extends AbstractClassCodeMining {

	private Object lock = new Object();

	public ClassReferenceCodeMining(String className, int afterLineNumber, IDocument document, ICodeMiningProvider provider)
			throws BadLocationException {
		super(className, afterLineNumber, document, provider);
	}

	@Override
	protected CompletableFuture<Void> doResolve(ITextViewer viewer, IProgressMonitor monitor) {
		return CompletableFuture.runAsync(() -> {
			IDocument document = viewer.getDocument();
			String className = super.getClassName();
			try {
				int wait = Integer.parseInt(className);
				try {
					for (int i = 0; i < wait; i++) {
						monitor.isCanceled();
						synchronized (lock) {
							lock.wait(1000);
						}
					}
				} catch (InterruptedException e) {
					Thread.currentThread().interrupt();
				}
			} catch (NumberFormatException e) {

			} catch (CancellationException e) {
				e.printStackTrace();
				throw e;
			}

			int refCount = 0;
			int lineCount = document.getNumberOfLines();
			for (int i = 0; i < lineCount; i++) {
				// check if request was canceled.
				monitor.isCanceled();
				String line = getLineText(document, i);
				refCount += line.contains("new " + className) ? 1 : 0;
			}
			super.setLabel(refCount + " references");
		});
	}

}

Back to the top