How do I add source links in my scaladoc?

405 views
Skip to first unread message

Scott Morrison

unread,
Jan 16, 2012, 3:40:15 PM1/16/12
to scala-user
Could someone point me to a recipe showing me how to have "sbt doc"
produce Scaladoc output that includes a link to sources?

Googling is failing me. I suspect that there is simply an option I can
put in my build.sbt, but the sbt documentation doesn't seem to say
anything about scaladoc.

thanks,
Scott Morrison

Chris Twiner

unread,
Jan 16, 2012, 3:57:30 PM1/16/12
to Scott Morrison, scala-user

Lol, should have read this one first. I had a plugin for 0.7 but I am only just working on 0.1x now.  I'll chat on the sbt group after I've done it.

Sbt questions are probably better off being asked on the sbt mailing list though.

Jason Zaugg

unread,
Jan 16, 2012, 5:18:00 PM1/16/12
to Chris Twiner, Scott Morrison, scala-user
You need a combination of two command line options to the scaladoc tool. First, -doc-source-url provides a URL template, and second -sourcepath establishes the base directory for the project.

I've just added support for this to the Scalaz build [1]

In `standardSettings`, which is shared by *all* projects in that build, I added:

  scalacOptions in (Compile, doc) <++= (baseDirectory in LocalProject("scalaz")).map {
      bd => Seq(
        "-sourcepath", 
        bd.getAbsolutePath, 
        "-doc-source-url", 
        "https://github.com/scalaz/scalaz/tree/scalaz-seven€{FILE_PATH}.scala")
  }

Dissecting this a bit:

1. we're configuring the key `compile:scalac-options(for doc)`. By default, this takes on the value of `compile:scalac-options`.
2. <++= means we need to access the value of other keys to initialize this one, and we are appending a sequence of additional elements to the previous value associated with this key. 
3. `baseDirectory in in LocalProject("scalaz")` refers to the parent project; I have to do it this way rather than simply referring to `baseDirectory in scalaz`, otherwise there would be a circular reference reported by the scala compiler when loading the build definition.

-jason

Chris Twiner

unread,
Jan 16, 2012, 5:39:56 PM1/16/12
to Jason Zaugg, Scott Morrison, scala-user

Thats nice, but isn't great for offline viewing, a bulk replace of a
dummy url token fixes that up nicely.

Chris Twiner

unread,
Jan 21, 2012, 6:52:49 AM1/21/12
to Jason Zaugg, Scott Morrison, scala-user

to fill that out a bit, I've made a partial migration and added
handling of multi-sub projects:

https://github.com/chris-twiner/scalesXml/blob/fullDocs/project/FullDocProject.scala

This approach uses sxr to display hyperlinked code and does a "smart"
replace. Many thanks to Jason for the fullDocs approach itself. The
benefit being that it can be used offline locally and packaged or via
a central website as well.

I have to make it less chatty (turn the content to debug only form
repointSxr) and handle .java files (not just scala ones) - but I'll
get round to that in the next days - I want at least the combined docs
and sxr for the Scales Xml 0.3 release.

Michael Slinn

unread,
Mar 4, 2012, 2:32:50 PM3/4/12
to scala...@googlegroups.com, Jason Zaugg, Scott Morrison
I want links to source in my project. My project only has build.sbt in the top directory. I don't want a whole lot of SBT code, so I just tried the following:

1) Created project/build.scala and put the following in it:

scalacOptions in (Compile, doc) <++= (baseDirectory in LocalProject("hanuman")).map {
  bd => Seq("-sourcepath", bd.getAbsolutePath, 
            "-doc-source-url", "https://github.com/mslinn/hanuman€{FILE_PATH}.scala")
}

(no idea what the Euro symbol in the 3rd line does, just copied it from the example).

2) Ran sbt doc:

$ sbt doc
[info] Loading global plugins from C:\Users\Mike Slinn\.sbt\plugins
[info] Loading project definition from E:\work\hanuman\project
[info] Compiling 1 Scala source to E:\work\hanuman\project\target\scala-2.9.1\sbt-0.11.2\classes...
[error] IO error while decoding E:\work\hanuman\project\build.scala with UTF-8
[error] Please try specifying another one using the -encoding option
[error] one error found
[error] {file:/E:/work/hanuman/project/}default-5e0f21/compile:compile: Compilation failed
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore?

3) I removed the Euro symbol and retried:

$ sbt doc
[info] Loading global plugins from C:\Users\Mike Slinn\.sbt\plugins
[info] Loading project definition from E:\work\hanuman\project
[info] Compiling 1 Scala source to E:\work\hanuman\project\target\scala-2.9.1\sbt-0.11.2\classes...
[error] E:\work\hanuman\project\build.scala:1: expected class or object definition
[error] scalacOptions in (Compile, doc) <++= (baseDirectory in LocalProject(" hanuman ")).map {
[error] ^
[error] one error found
[error] {file:/E:/work/hanuman/project/}default-5e0f21/compile:compile: Compilation failed
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore?

4) I put the code into project/build.sbt instead and got the same error.

Is there a way to do this without getting into a whole lot of code?

Mike

Jason Zaugg

unread,
Mar 4, 2012, 2:48:55 PM3/4/12
to Michael Slinn, scala...@googlegroups.com, Scott Morrison
On Sun, Mar 4, 2012 at 8:32 PM, Michael Slinn <msl...@gmail.com> wrote:
> I want links to source in my project. My project only has build.sbt in the
> top directory. I don't want a whole lot of SBT code, so I just tried the
> following:
>
> 1) Created project/build.scala and put the following in it:
>
> scalacOptions in (Compile, doc) <++= (baseDirectory in
> LocalProject("hanuman")).map {
>   bd => Seq("-sourcepath", bd.getAbsolutePath,
>             "-doc-source-url",
> "https://github.com/mslinn/hanuman€{FILE_PATH}.scala")
> }
>
> (no idea what the Euro symbol in the 3rd line does, just copied it from the
> example).

Your text editor and your SBT instance disagree [1] on the encoding of
the file. Either add -Dfile.encoding=UTF-8 to your SBT options, or
stick to ASCII and represent \u20AC rather than €. [2]

-jason

[1] https://github.com/scalaz/scalaz/issues/80#issuecomment-4312538
[2] http://www.joelonsoftware.com/articles/Unicode.html

Michael Slinn

unread,
Mar 4, 2012, 4:13:06 PM3/4/12
to scala...@googlegroups.com, Michael Slinn, Scott Morrison
Retronym clarified the Euro symbol usage. I'm still in the dark about how to generate source links without writing large chunks of SBT code. I had hoped to be able to do something simple, like this, but the "simple build tool" is anything but:

docOptions += "-doc-source-url https://github.com/mslinn/hanuman"
^
E:\work\hanuman\build.sbt:20: error: reassignment to val

Mike

Jason Zaugg

unread,
Mar 4, 2012, 5:25:36 PM3/4/12
to scala-user
[forwarding to list]

On Sun, Mar 4, 2012 at 11:24 PM, Jason Zaugg <jza...@gmail.com> wrote:
> The correct setting is:


>
>   scalacOptions in (Compile, doc) <++= (baseDirectory in

> LocalProject("myRootProject")).map {
>      (bd: File) => Seq[String](


>         "-sourcepath", bd.getAbsolutePath,
>         "-doc-source-url",

> "https://github.com/scalaz/scalaz/tree/scalaz-seven€{FILE_PATH}.scala"
>      )
>    }
>
> If you have a single module project, you could simplify to:
>
>   scalacOptions in (Compile, doc) <++= baseDirectory.map {
>      (bd: File) => Seq[String](


>         "-sourcepath", bd.getAbsolutePath,
>         "-doc-source-url",

> "https://github.com/scalaz/scalaz/tree/scalaz-seven€{FILE_PATH}.scala"
>      )
>    }
>
> -jason

Mike Slinn

unread,
Mar 4, 2012, 5:32:01 PM3/4/12
to scala...@googlegroups.com
Thanks Jason, that gave me enough information to make this work!

Mike

Message has been deleted

Paul Dao

unread,
Jan 28, 2014, 10:03:53 PM1/28/14
to scala...@googlegroups.com
Where are the scaladoc html file generated to?

Mike Slinn

unread,
Jan 28, 2014, 10:45:31 PM1/28/14
to Paul Dao, scala...@googlegroups.com
Since sbt 0.12, when there are only Java sources or only Scala sources but not both, the output directory is target/api/. When there are both Java and Scala sources present, the output directories are target/api/java target/api/scala.

Mike
Reply all
Reply to author
Forward
0 new messages