Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8a50029e7d1cdfedb32c09b51b478636dbbbc7a0 (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
/*******************************************************************************
 * Copyright (c) 2008, 2009 Broadcom 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
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     James Blackburn (Broadcom) - Initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.resources;

import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.core.model.CModelManager;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourceAttributes;

/**
 * This class computes a relevance for files in case we have to select
 * from multiple files for the same file-system location.
 */
public class FileRelevance {
	private static final int PREFERRED_PROJECT 		= 0x40;
	private static final int CDT_PROJECT 			= 0x20;
	private static final int ON_SOURCE_ROOT			= 0x10;

	// Penalty for undesirable attributes
	private static final int LINK_PENALTY			= 1;
	private static final int INACCESSIBLE_SHIFT 	= 4;

	/**
	 * Compute a relevance for the given file. The higher the score the more relevant the
	 * file. It is determined by the following criteria: <br>
	 * - file belongs to preferred project <br>
	 * - file belongs to a cdt-project <br>
	 * - file belongs to a source folder of a cdt-project <br>
	 * - file is accessible
	 * - file is not a link
	 * @param f the file to compute the relevance for
	 * @return integer representing file relevance. Larger numbers are more relevant
	 */
	public static int getRelevance(IFile f, IProject preferredProject) {
		int result= 0;
		IProject p= f.getProject();
		if (p.equals(preferredProject))
			result+= PREFERRED_PROJECT;

		if (CoreModel.hasCNature(p)) {
			result+= CDT_PROJECT;
			ICProject cproject= CModelManager.getDefault().create(p);
			if (cproject.isOnSourceRoot(f))
				result+= ON_SOURCE_ROOT;
		}

		if (!f.isAccessible())
			result >>= INACCESSIBLE_SHIFT;
		else {
			ResourceAttributes ra = f.getResourceAttributes();
			if (f.isLinked() || (ra != null && ra.isSymbolicLink()))
				result -= LINK_PENALTY;
		}

		return result;
	}
}

Back to the top