elm-format icon indicating copy to clipboard operation
elm-format copied to clipboard

Elm-format should distinguish between different types of comments.

Open amilner42 opened this issue 7 years ago • 6 comments

At the moment, if you have no imports, you cannot comment the top most function of your file. That comment will be pulled to the top of the file.

Example:

module Bla exposing (..)

-- I want to add a comment here
type alias Bla =
    { }

but elm format will make this

module Bla exposing (..)

-- I want to add a comment here


type alias Bla =
    { }

If it's a doc-comment, {-| -}, it makes sense to pull it to the top because it only makes sense to have doc comments above functions if you have a doc-comment in the module as well. But it doesn't make sense to be pulling -- and {- -} to the top of the file. We should be able to comment the top most function of our files even if we have no imports.

amilner42 avatar Jun 16 '17 19:06 amilner42

To elaborate, there seems to be a few more issues with the top comment. For example if I do have imports, then my first comment can be a doc-comment, but it can only be a doc-comment.

module Bla exposing (..)

import X


{-| Allowed
-}
type alias Bla =
    { }

But both of these will be reformatted:

module Bla exposing (..)

import X


-- This comment will be pushed up upon formatting
type alias Bla =
    { }
module Bla exposing (..)

import X


{- This comment will be pushed up upon formatting -}
type alias Bla =
    { }

amilner42 avatar Jun 16 '17 19:06 amilner42

This is a case where elm-format is making the behavior of elm-make/elm-compiler more clear: If a module has no imports, you cannot have a doc comment on the first definition unless you also have a doc comment for the module before it--this is due to the behavior of elm-make and how it parses documentation. elm-format adjusts the spacing to make it more obvious how elm-make will interpret your doc comments.

avh4 avatar Jul 24 '17 06:07 avh4

Is anyone considering this and is there a work-around in the meantime? elm-format is moving my comments away from the code I mean to document.

sjfloat avatar Aug 31 '21 14:08 sjfloat

@sjfloat There are workarounds! If you show your code – here or in Slack – you could get tips on how to do it.

lydell avatar Aug 31 '21 15:08 lydell

I really don't want my comments to be altered at all. It almost always subverts my intention. But @amilner42's original example is a good starting point. How do I simply keep the comment where I originally put it, near the material that I'm referring to in the comment? Or how do I prevent a commented line from being moved to somewhere else?

sjfloat avatar Aug 31 '21 16:08 sjfloat

(Your previous comment contains two questions – I only replied to “is there a workaround in the meantime?”)

Edit: Oops, read you reply too quick. The workaround is to use a doc comment like they show.

lydell avatar Aug 31 '21 16:08 lydell