001 /* 002 * Copyright 2010-2012 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.js.compiler; 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.Nullable; 024 import org.jetbrains.js.compiler.sourcemap.SourceMapBuilder; 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(JsProgramFragment x) { 042 x.acceptChildren(this); 043 } 044 045 @Override 046 public void visitBlock(JsBlock x) { 047 printJsBlock(x, false, true); 048 } 049 050 @Override 051 public void newLined() { 052 if (sourceMapBuilder != null) { 053 sourceMapBuilder.newLine(); 054 } 055 } 056 057 @Override 058 public void indentedAfterNewLine() { 059 if (pendingSources.isEmpty()) return; 060 061 assert sourceMapBuilder != null; 062 for (Object source : pendingSources) { 063 sourceMapBuilder.processSourceInfo(source); 064 } 065 pendingSources.clear(); 066 } 067 068 @Override 069 public void accept(JsNode node) { 070 if (!(node instanceof JsNameRef) && !(node instanceof JsLiteral.JsThisRef)) { 071 mapSource(node); 072 } 073 super.accept(node); 074 } 075 076 private void mapSource(JsNode node) { 077 if (sourceMapBuilder != null) { 078 Object sourceInfo = node.getSource(); 079 if (sourceInfo != null) { 080 if (p.isJustNewlined()) { 081 pendingSources.add(sourceInfo); 082 } 083 else { 084 sourceMapBuilder.processSourceInfo(sourceInfo); 085 } 086 } 087 } 088 } 089 090 @Override 091 protected void beforeNodePrinted(JsNode node) { 092 mapSource(node); 093 } 094 095 @Override 096 public void visitProgram(JsProgram program) { 097 program.acceptChildren(this); 098 if (sourceMapBuilder != null) { 099 sourceMapBuilder.addLink(); 100 } 101 } 102 }