001 /* 002 * Copyright 2010-2015 JetBrains s.r.o. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.jetbrains.kotlin.js.sourceMap; 018 019 import com.google.dart.compiler.backend.js.JsToStringGenerationVisitor; 020 import com.google.dart.compiler.backend.js.ast.*; 021 import com.google.dart.compiler.util.TextOutput; 022 import com.intellij.util.SmartList; 023 import org.jetbrains.annotations.NotNull; 024 import org.jetbrains.annotations.Nullable; 025 026 import java.util.List; 027 028 public class JsSourceGenerationVisitor extends JsToStringGenerationVisitor implements TextOutput.OutListener { 029 @Nullable 030 private final SourceMapBuilder sourceMapBuilder; 031 032 private final List<Object> pendingSources = new SmartList<Object>(); 033 034 public JsSourceGenerationVisitor(TextOutput out, @Nullable SourceMapBuilder sourceMapBuilder) { 035 super(out); 036 this.sourceMapBuilder = sourceMapBuilder; 037 out.setOutListener(this); 038 } 039 040 @Override 041 public void visitProgramFragment(@NotNull JsProgramFragment x) { 042 x.acceptChildren(this); 043 } 044 045 @Override 046 public void newLined() { 047 if (sourceMapBuilder != null) { 048 sourceMapBuilder.newLine(); 049 } 050 } 051 052 @Override 053 public void indentedAfterNewLine() { 054 if (pendingSources.isEmpty()) return; 055 056 assert sourceMapBuilder != null; 057 for (Object source : pendingSources) { 058 sourceMapBuilder.processSourceInfo(source); 059 } 060 pendingSources.clear(); 061 } 062 063 @Override 064 public void accept(JsNode node) { 065 if (!(node instanceof JsNameRef) && !(node instanceof JsLiteral.JsThisRef)) { 066 mapSource(node); 067 } 068 super.accept(node); 069 } 070 071 private void mapSource(JsNode node) { 072 if (sourceMapBuilder != null) { 073 Object sourceInfo = node.getSource(); 074 if (sourceInfo != null) { 075 if (p.isJustNewlined()) { 076 pendingSources.add(sourceInfo); 077 } 078 else { 079 sourceMapBuilder.processSourceInfo(sourceInfo); 080 } 081 } 082 } 083 } 084 085 @Override 086 protected void beforeNodePrinted(JsNode node) { 087 mapSource(node); 088 } 089 090 @Override 091 public void visitProgram(@NotNull JsProgram program) { 092 program.acceptChildren(this); 093 if (sourceMapBuilder != null) { 094 sourceMapBuilder.addLink(); 095 } 096 } 097 }