summaryrefslogtreecommitdiff
path: root/platform/script-debugger/backend/src/org/jetbrains/debugger/sourcemap/SourceMap.java
blob: 057d96fd01eb0f63bb760e0190e871fd206d762a (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
package org.jetbrains.debugger.sourcemap;

import com.intellij.openapi.util.NullableLazyValue;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.Processor;
import com.intellij.util.Url;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class SourceMap {
  private final MappingList mappings;
  final MappingList[] sourceIndexToMappings;

  private final String outFile;

  private final SourceResolver sourceResolver;
  private final boolean hasNameMappings;

  // sources - is not originally specified, but canonicalized/normalized
  public SourceMap(@Nullable String outFile,
                   @NotNull MappingList mappings,
                   @NotNull MappingList[] sourceIndexToMappings,
                   @NotNull SourceResolver sourceResolver,
                   boolean hasNameMappings) {
    this.outFile = outFile;
    this.mappings = mappings;
    this.sourceIndexToMappings = sourceIndexToMappings;
    this.sourceResolver = sourceResolver;
    this.hasNameMappings = hasNameMappings;
  }

  public boolean hasNameMappings() {
    return hasNameMappings;
  }

  @NotNull
  public SourceResolver getSourceResolver() {
    return sourceResolver;
  }

  @Nullable
  public String getOutFile() {
    return outFile;
  }

  public Url[] getSources() {
    return sourceResolver.canonicalizedSources;
  }

  @NotNull
  public MappingList getMappings() {
    return mappings;
  }

  public int getSourceLineByRawLocation(int rawLine, int rawColumn) {
    MappingEntry entry = getMappings().get(rawLine, rawColumn);
    return entry == null ? -1: entry.getSourceLine();
  }

  public boolean processMappingsInLine(@NotNull List<Url> sourceUrls,
                                       int sourceLine,
                                       @NotNull Processor<MappingEntry> mappingProcessor,
                                       @Nullable VirtualFile sourceFile,
                                       @Nullable NullableLazyValue<SourceResolver.Resolver> resolver) {
    MappingList mappings = sourceResolver.findMappings(sourceUrls, this, sourceFile);
    if (mappings == null) {
      if (resolver != null) {
        SourceResolver.Resolver resolverValue = resolver.getValue();
        if (resolverValue != null) {
          mappings = sourceResolver.findMappings(sourceFile, this, resolverValue);
        }
      }
      if (mappings == null) {
        return false;
      }
    }
    return mappings.processMappingsInLine(sourceLine, mappingProcessor);
  }

  @NotNull
  public MappingList getMappingsOrderedBySource(int source) {
    return sourceIndexToMappings[source];
  }
}