Interface Scalafmt


  • public interface Scalafmt
    A stable public interface to run Scalafmt. This interface is designed for integrations such as editor plugins and build tools. An implementation of this interface is available in the module 'scalafmt-dynamic'. It is recommended to use this interface over the org.scalafmt.Scalafmt object for several reasons: - this API is guaranteed to be binary compatible with all future versions of Scalafmt. - it downloads the Scalafmt version matching the 'version' setting in .scalafmt.conf. All versions down to v1.2.0 are supported. - it respects the 'project.{excludeFilters,includeFilters}' setting in .scalafmt.conf. - it uses the correct parser for `*.sbt` and `*.sc` files. - it automatically caches parsing of configuration files avoiding redundant work when possible. - it has two external library dependencies (com.geirsson:coursier-small and com.typesafe:config), which is a smaller dependency footprint compared to scalafmt-core.
    • Method Detail

      • format

        java.lang.String format​(java.nio.file.Path config,
                                java.nio.file.Path file,
                                java.lang.String code)
        Format a single file with the given .scalafmt.conf configuration.
        Parameters:
        config - the absolute path to the configuration file. This file must exist or an exception will be thrown.
        file - relative or absolute path to the file being formatted. Used only for the path name, the file does not have to exist on disk.
        code - the text contents to format.
        Returns:
        the formatted contents if formatting was successful, otherwise the original text contents.
      • withRespectProjectFilters

        Scalafmt withRespectProjectFilters​(boolean respectExcludeFilters)
        Builder method.
        Parameters:
        respectExcludeFilters - If true (default), returns the original file contents when formatting a file that does not matches the project settings in .scalafmt.conf. If false, formats every file that is passed to the format(Path, Path, String) method regardless of .scalafmt.conf settings.
        Returns:
        an updated interface instance controlling whether to respect the 'project.{excludeFilters,includeFilters}' setting in .scalafmt.conf.
      • withRespectVersion

        @Deprecated
        Scalafmt withRespectVersion​(boolean respectVersion)
        Deprecated.
        Builder method.
        Parameters:
        respectVersion - If true (default), refuses to format files when the 'version' setting is missing in .scalafmt.conf and users must update .scalafmt.conf to format files. If false, falls back to the default version provided via withDefaultVersion(String).
        Returns:
        an updated interface instance controlling whether to respect the 'version' setting in .scalafmt.conf.
      • withDefaultVersion

        @Deprecated
        Scalafmt withDefaultVersion​(java.lang.String defaultVersion)
        Deprecated.
        Builder method.
        Parameters:
        defaultVersion - the fallback Scalafmt version to use when there is no 'version' setting in `.scalafmt.conf`; N.B. ignored when withRespectVersion(boolean) is true
        Returns:
        an updated interface instance with the default version set
      • withReporter

        Scalafmt withReporter​(ScalafmtReporter reporter)
        Builder method.
        Parameters:
        reporter - Use this instance to report errors and information messages
        Returns:
        an updated interface instance with the reporter instance set
      • withMavenRepositories

        Scalafmt withMavenRepositories​(java.lang.String... repositories)
        Builder method.
        Parameters:
        repositories - maven repositories to use when resolving
        Returns:
        an updated interface instance with the repositories to use to resolve dependencies.
      • withRepositoryCredentials

        Scalafmt withRepositoryCredentials​(RepositoryCredential... credentials)
        Builder method.
        Parameters:
        credentials - repository credentials to use when resolving
        Returns:
        an updated interface instance.
      • clear

        void clear()
        Clear internal caches such as classloaded Scalafmt instances.
      • create

        static Scalafmt create​(java.lang.ClassLoader loader)
        Classload a new instance of this Scalafmt instance.
        Parameters:
        loader - the classloader containing the 'scalafmt-dynamic' module.
        Returns:
        a new instance of this interface
        Throws:
        java.util.NoSuchElementException - if the classloader does not have the 'scalafmt-dynamic' module.
      • createSession

        ScalafmtSession createSession​(java.nio.file.Path config)
        Create a ScalafmtSession to format a batch of files using fixed configuration.
        Parameters:
        config - location of the configuration file
        Returns:
        a new session instance