Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styleguide settings for Kotlin

I've started to use Koltin heavily and struggling a bit with code formatting.

Although there is an official coding conventions guide, but I wonder if there is any public available styleguide settings for IntelliJ (such as google codestyle for Java) which you can grab/import and use?

like image 740
vtor Avatar asked Sep 15 '16 13:09

vtor


People also ask

Does indentation matter in Kotlin?

In Kotlin coding conventions, it's recommended to use a single indent in cases where the long continuation indent has been forced before. In practice, quite a bit of code is affected, so this can be considered a major code style update.

How do you name a Kotlin file?

Naming. If a source file contains only a single top-level class, the file name should reflect the case-sensitive name plus the . kt extension. Otherwise, if a source file contains multiple top-level declarations, choose a name that describes the contents of the file, apply PascalCase, and append the .

How do you add comments on Kotlin?

Single-line comments starts with two forward slashes ( // ). Any text between // and the end of the line is ignored by Kotlin (will not be executed).


1 Answers

There's no need to grab/import them they are built into IntelliJ IDEA's Kotlin language support plugin.

You can reformat Kotlin source code just like you would code written in Java or other supported languages.

From IntelliJ IDEA 2016.2 Help :: Reformatting Source Code:

Basics

IntelliJ IDEA lets you reformat source code to meet the requirements of your code style. IntelliJ IDEA will lay out spacing, indents, keywords etc. Reformatting can apply to the selected text, entire file, or entire project.

It is also possible to apply reformatting to the parts of the source code only, using the formatting markers.

Reformatting code

To reformat code for a directory, follow these steps:

  1. In the Project tool window, select a module or directory you want to apply your reformatting to.
  2. From the main menu, select Code | Reformat Code or press Ctrl+Alt+L.

    Alternatively, in the Project tool window, right-click the directory and from the context menu, select Reformat Code.

  3. In the Reformat Code dialog box, specify the necessary options and filters for you reformatting and click Run.

Reformatting code for the current file

To reformat code for the current file, follow these steps:

  1. In the editor of the currently opened file, press Ctrl+Shift+Alt+L.

    Note that if you select Code | Reformat Code from the main menu or press Ctrl+Alt+L, IntelliJ IDEA will try to reformat the source code automatically without opening the Reformat File dialog.

  2. In the Reformat File dialog, specify options for the reformatting and click Run.
like image 69
mfulton26 Avatar answered Oct 16 '22 17:10

mfulton26