1 /* 2 * Copyright 2015 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.googlejavaformat; 16 17 import com.google.common.base.MoreObjects; 18 import com.google.common.collect.Range; 19 import com.google.googlejavaformat.OpsBuilder.BlankLineWanted; 20 import java.util.Optional; 21 22 /** An output from the formatter. */ 23 public abstract class Output extends InputOutput { 24 /** Unique identifier for a break. */ 25 public static final class BreakTag { 26 27 Optional<Boolean> taken = Optional.empty(); 28 recordBroken(boolean broken)29 public void recordBroken(boolean broken) { 30 // TODO(cushon): enforce invariants. 31 // Currently we rely on setting Breaks multiple times, e.g. when deciding 32 // whether a Level should be flowed. Using separate data structures 33 // instead of mutation or adding an explicit 'reset' step would allow 34 // a useful invariant to be enforced here. 35 taken = Optional.of(broken); 36 } 37 wasBreakTaken()38 public boolean wasBreakTaken() { 39 return taken.orElse(false); 40 } 41 } 42 43 /** 44 * Indent by outputting {@code indent} spaces. 45 * 46 * @param indent the current indent 47 */ indent(int indent)48 public abstract void indent(int indent); 49 50 /** 51 * Output a string. 52 * 53 * @param text the string 54 * @param range the {@link Range} corresponding to the string 55 */ append(String text, Range<Integer> range)56 public abstract void append(String text, Range<Integer> range); 57 58 /** 59 * A blank line is or is not wanted here. 60 * 61 * @param k the {@link Input.Tok} index 62 * @param wanted whether a blank line is wanted here 63 */ blankLine(int k, BlankLineWanted wanted)64 public abstract void blankLine(int k, BlankLineWanted wanted); 65 66 /** Marks a region that can be partially formatted. */ markForPartialFormat(Input.Token start, Input.Token end)67 public abstract void markForPartialFormat(Input.Token start, Input.Token end); 68 69 /** 70 * Get the {@link CommentsHelper}. 71 * 72 * @return the {@link CommentsHelper} 73 */ getCommentsHelper()74 public abstract CommentsHelper getCommentsHelper(); 75 76 @Override toString()77 public String toString() { 78 return MoreObjects.toStringHelper(this).add("super", super.toString()).toString(); 79 } 80 } 81