aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/skylark/java/com/google/devtools/skylark/skylint/LocationRange.java
blob: 6f1c2deb98f756c8dce1a80f2c23266b3b96f16e (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
// Copyright 2017 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.skylark.skylint;

import com.google.common.base.Preconditions;
import com.google.devtools.build.lib.events.Location.LineAndColumn;
import javax.annotation.Nullable;

/**
 * Location range of a linter warning.
 *
 * <p>It is a closed interval: [start, end], i.e. the end location is the last character in the
 * range.
 */
public class LocationRange {
  public final Location start;
  public final Location end;

  public LocationRange(Location start, Location end) {
    Preconditions.checkArgument(
        Location.compare(start, end) <= 0,
        "end location (%s) should be after start location (%s)",
        end,
        start);
    this.start = start;
    this.end = end;
  }

  public static LocationRange from(com.google.devtools.build.lib.events.Location location) {
    Location start = Location.from(location.getStartLineAndColumn());
    Location end = Location.from(location.getEndLineAndColumn());
    return new LocationRange(start, end);
  }

  public static int compare(LocationRange l1, LocationRange l2) {
    int cmp = Location.compare(l1.start, l2.start);
    if (cmp != 0) {
      return cmp;
    }
    return Location.compare(l1.end, l2.end);
  }

  @Override
  public String toString() {
    return start + "-" + end;
  }

  /** Single location in a file (line + column). */
  public static class Location {
    public final int line;
    public final int column;

    public Location(int line, int column) {
      this.line = line;
      this.column = column;
    }

    public static Location from(@Nullable LineAndColumn lac) {
      // LineAndColumn may be null, e.g. if a BuildFileAST contains no statements:
      if (lac == null) {
        return new Location(1, 1);
      }
      return new Location(lac.getLine(), lac.getColumn());
    }

    public static int compare(Location l1, Location l2) {
      int cmp = l1.line - l2.line;
      if (cmp != 0) {
        return cmp;
      }
      return l1.column - l2.column;
    }

    @Override
    public String toString() {
      return line + ":" + column;
    }
  }
}