|
| 1 | +package org.scalafmt.interfaces |
| 2 | + |
| 3 | +import java.nio.file.Path |
| 4 | +import java.util |
| 5 | +import java.util.NoSuchElementException |
| 6 | +import java.util.ServiceLoader |
| 7 | + |
| 8 | +/** A stable public interface to run Scalafmt. |
| 9 | + * |
| 10 | + * This interface is designed for integrations such as editor plugins and build |
| 11 | + * tools. An implementation of this interface is available in the module |
| 12 | + * 'scalafmt-dynamic'. |
| 13 | + * |
| 14 | + * It is recommended to use this interface over the org.scalafmt.Scalafmt |
| 15 | + * object for several reasons: |
| 16 | + * |
| 17 | + * - this API is guaranteed to be binary compatible with all future versions |
| 18 | + * of Scalafmt. |
| 19 | + * - it downloads the Scalafmt version matching the 'version' setting in |
| 20 | + * .scalafmt.conf. All versions down to v1.2.0 are supported. |
| 21 | + * - it respects the 'project.{excludeFilters,includeFilters}' setting in |
| 22 | + * .scalafmt.conf. |
| 23 | + * - it uses the correct parser for `*.sbt` and `*.sc` files. |
| 24 | + * - it automatically caches parsing of configuration files avoiding |
| 25 | + * redundant work when possible. |
| 26 | + * - it has two external library dependencies (com.geirsson:coursier-small |
| 27 | + * and com.typesafe:config), which is a smaller dependency footprint |
| 28 | + * compared to scalafmt-core. |
| 29 | + */ |
| 30 | +trait Scalafmt { |
| 31 | + |
| 32 | + /** Format a single file with the given .scalafmt.conf configuration. |
| 33 | + * |
| 34 | + * @param config |
| 35 | + * the absolute path to the configuration file. This file must exist or an |
| 36 | + * exception will be thrown. |
| 37 | + * @param file |
| 38 | + * relative or absolute path to the file being formatted. Used only for the |
| 39 | + * path name, the file does not have to exist on disk. |
| 40 | + * @param code |
| 41 | + * the text contents to format. |
| 42 | + * @return |
| 43 | + * the formatted contents if formatting was successful, otherwise the |
| 44 | + * original text contents. |
| 45 | + */ |
| 46 | + def format(config: Path, file: Path, code: String): String |
| 47 | + |
| 48 | + /** Builder method. |
| 49 | + * |
| 50 | + * @param respectExcludeFilters |
| 51 | + * If true (default), returns the original file contents when formatting a |
| 52 | + * file that does not matches the project settings in .scalafmt.conf. If |
| 53 | + * false, formats every file that is passed to the |
| 54 | + * {@link #format(Path, Path, String)} method regardless of .scalafmt.conf |
| 55 | + * settings. |
| 56 | + * @return |
| 57 | + * an updated interface instance controlling whether to respect the |
| 58 | + * 'project.{excludeFilters,includeFilters}' setting in .scalafmt.conf. |
| 59 | + */ |
| 60 | + def withRespectProjectFilters(respectExcludeFilters: Boolean): Scalafmt |
| 61 | + |
| 62 | + /** Builder method. |
| 63 | + * |
| 64 | + * @param respectVersion |
| 65 | + * If true (default), refuses to format files when the 'version' setting is |
| 66 | + * missing in .scalafmt.conf and users must update .scalafmt.conf to format |
| 67 | + * files. If false, falls back to the default version provided via |
| 68 | + * {@link #withDefaultVersion(String)} . |
| 69 | + * @return |
| 70 | + * an updated interface instance controlling whether to respect the |
| 71 | + * 'version' setting in .scalafmt.conf. |
| 72 | + */ |
| 73 | + @Deprecated |
| 74 | + def withRespectVersion(respectVersion: Boolean): Scalafmt |
| 75 | + |
| 76 | + /** Builder method. |
| 77 | + * |
| 78 | + * @param defaultVersion |
| 79 | + * the fallback Scalafmt version to use when there is no 'version' setting |
| 80 | + * in `.scalafmt.conf`; N.B. ignored when |
| 81 | + * {@link #withRespectVersion(boolean)} is true |
| 82 | + * @return |
| 83 | + * an updated interface instance with the default version set |
| 84 | + */ |
| 85 | + @Deprecated |
| 86 | + def withDefaultVersion(defaultVersion: String): Scalafmt |
| 87 | + |
| 88 | + /** Builder method. |
| 89 | + * |
| 90 | + * @param reporter |
| 91 | + * Use this instance to report errors and information messages |
| 92 | + * @return |
| 93 | + * an updated interface instance with the reporter instance set |
| 94 | + */ |
| 95 | + def withReporter(reporter: ScalafmtReporter): Scalafmt |
| 96 | + |
| 97 | + /** Builder method. |
| 98 | + * |
| 99 | + * @param repositories |
| 100 | + * maven repositories to use when resolving |
| 101 | + * @return |
| 102 | + * an updated interface instance with the repositories to use to resolve |
| 103 | + * dependencies. |
| 104 | + */ |
| 105 | + def withMavenRepositories(repositories: String*): Scalafmt |
| 106 | + |
| 107 | + /** Builder method. |
| 108 | + * |
| 109 | + * @param credentials |
| 110 | + * repository credentials to use when resolving |
| 111 | + * @return |
| 112 | + * an updated interface instance. |
| 113 | + */ |
| 114 | + def withRepositoryCredentials(credentials: RepositoryCredential*): Scalafmt |
| 115 | + |
| 116 | + /** Clear internal caches such as classloaded Scalafmt instances. |
| 117 | + */ |
| 118 | + def clear(): Unit |
| 119 | + |
| 120 | + /** Create a ScalafmtSession to format a batch of files using fixed |
| 121 | + * configuration. |
| 122 | + * @param config |
| 123 | + * location of the configuration file |
| 124 | + * @return |
| 125 | + * a new session instance |
| 126 | + */ |
| 127 | + def createSession(config: Path): ScalafmtSession |
| 128 | +} |
| 129 | + |
| 130 | +object Scalafmt { |
| 131 | + |
| 132 | + /** Classload a new instance of this Scalafmt instance. |
| 133 | + * |
| 134 | + * @param loader |
| 135 | + * the classloader containing the 'scalafmt-dynamic' module. |
| 136 | + * @return |
| 137 | + * a new instance of this interface |
| 138 | + * @throws NoSuchElementException |
| 139 | + * if the classloader does not have the 'scalafmt-dynamic' module. |
| 140 | + */ |
| 141 | + def create(loader: ClassLoader): Scalafmt = throw new ScalafmtException( |
| 142 | + "Can't use different version for native CLI", |
| 143 | + null, |
| 144 | + ) |
| 145 | +} |
0 commit comments