aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffDriver.java
blob: b74444400ed98c23efcc46da8047520bd803d5f1 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright (c) 2024 Qualcomm Innovation Center, Inc.
 * and other copyright owners as documented in the project's IP log.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.diff;

import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * Built-in drivers for various languages, sorted by name. These drivers will be
 * used to determine function names for a hunk.
 * <p>
 * When writing or updating patterns, assume the contents are syntactically
 * correct. Patterns can be simple and need not cover all syntactical corner
 * cases, as long as they are sufficiently permissive.
 *
 * @since 6.10.1
 */
@SuppressWarnings({"ImmutableEnumChecker", "nls"})
public enum DiffDriver {
	/**
	 * Built-in diff driver for <a href=
	 * "https://learn.microsoft.com/en-us/cpp/cpp/cpp-language-reference">c++</a>
	 */
	cpp(List.of(
			/* Jump targets or access declarations */
			"^[ \\t]*[A-Za-z_][A-Za-z_0-9]*:\\s*($|/[/*])"), List.of(
			/* functions/methods, variables, and compounds at top level */
			"^((::\\s*)?[A-Za-z_].*)$")),
	/**
	 * Built-in diff driver for <a href=
	 * "https://devicetree-specification.readthedocs.io/en/stable/source-language.html">device
	 * tree files</a>
	 */
	dts(List.of(";", "="), List.of(
			/* lines beginning with a word optionally preceded by '&' or the root */
			"^[ \\t]*((/[ \\t]*\\{|&?[a-zA-Z_]).*)")),
	/**
	 * Built-in diff driver for <a href=
	 * "https://docs.oracle.com/javase/specs/jls/se21/html/index.html">java</a>
	 */
	java(List.of(
			"^[ \\t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)"),
			List.of(
					/* Class, enum, interface, and record declarations */
					"^[ \\t]*(([a-z-]+[ \\t]+)*(class|enum|interface|record)[ \\t]+.*)$",
					/* Method definitions; note that constructor signatures are not */
					/* matched because they are indistinguishable from method calls. */
					"^[ \\t]*(([A-Za-z_<>&\\]\\[][?&<>.,A-Za-z_0-9]*[ \\t]+)+[A-Za-z_]"
							+ "[A-Za-z_0-9]*[ \\t]*\\([^;]*)$")),
	/**
	 * Built-in diff driver for
	 * <a href="https://docs.python.org/3/reference/index.html">python</a>
	 */
	python(List.of("^[ \\t]*((class|(async[ \\t]+)?def)[ \\t].*)$")),
	/**
	 * Built-in diff driver for
	 * <a href="https://doc.rust-lang.org/reference/introduction.html">rust</a>
	 */
	rust(List.of("^[\\t ]*((pub(\\([^\\)]+\\))?[\\t ]+)?"
			+ "((async|const|unsafe|extern([\\t ]+\"[^\"]+\"))[\\t ]+)?"
			+ "(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \\t]+[^;]*)$"));

	private final List<Pattern> negatePatterns;

	private final List<Pattern> matchPatterns;

	DiffDriver(List<String> negate, List<String> match, int flags) {
		if (negate != null) {
			this.negatePatterns = negate.stream()
					.map(r -> Pattern.compile(r, flags))
					.collect(Collectors.toList());
		} else {
			this.negatePatterns = null;
		}
		this.matchPatterns = match.stream().map(r -> Pattern.compile(r, flags))
				.collect(Collectors.toList());
	}

	DiffDriver(List<String> match) {
		this(null, match, 0);
	}

	DiffDriver(List<String> negate, List<String> match) {
		this(negate, match, 0);
	}

	/**
	 * Returns the list of patterns used to exclude certain lines from being
	 * considered as function names.
	 *
	 * @return the list of negate patterns
	 */
	public List<Pattern> getNegatePatterns() {
		return negatePatterns;
	}

	/**
	 * Returns the list of patterns used to match lines for potential function
	 * names.
	 *
	 * @return the list of match patterns
	 */
	public List<Pattern> getMatchPatterns() {
		return matchPatterns;
	}
}