PageRenderTime 8ms CodeModel.GetById 2ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-core/src/main/scala/org/fusesource/scalate/console/EditLink.scala

http://github.com/scalate/scalate
Scala | 92 lines | 53 code | 18 blank | 21 comment | 13 complexity | 1792ffd0b9c63117199ad541c499b8af MD5 | raw file
 1/**
 2 * Copyright (C) 2009-2011 the original author or authors.
 3 * See the notice.md file distributed with this work for additional
 4 * information regarding copyright ownership.
 5 *
 6 * Licensed under the Apache License, Version 2.0 (the "License");
 7 * you may not use this file except in compliance with the License.
 8 * You may obtain a copy of the License at
 9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.fusesource.scalate.console
19
20import java.io.File
21
22import org.fusesource.scalate.RenderContext.captureNodeSeq
23
24import scala.xml.NodeSeq
25
26/**
27 * @version $Revision : 1.1 $
28 */
29
30object EditLink {
31
32  var idePluginPort = 51235
33
34  def editLink(file: String)(body: => Unit): NodeSeq = editLink(file, None, None)(body)
35
36  def editLink(file: String, line: Option[Int], col: Option[Int])(body: => Unit): NodeSeq = {
37    if (file == null) {
38      Nil
39    } else {
40      System.getProperty("scalate.editor", "") match {
41        case "textmate" => editLinkTextMate(file, line, col)(body)
42        case "ide" => editLinkIdePlugin(file, line, col)(body)
43        case "file" => editLinkFileScheme(file, line, col)(body)
44        case _ =>
45          if (isMacOsx && hasTextMate)
46            editLinkTextMate(file, line, col)(body)
47          else {
48            editLinkFileScheme(file, line, col)(body)
49          }
50      }
51    }
52  }
53
54  def editLinkFileScheme(file: String, line: Option[Int], col: Option[Int])(body: => Unit): NodeSeq = {
55    val bodyText = captureNodeSeq(body)
56    <a href={ "file://" + file } title="Open File" target="_blank">
57      { bodyText }
58    </a>
59  }
60
61  def editLinkTextMate(file: String, line: Option[Int], col: Option[Int])(body: => Unit): NodeSeq = {
62    val bodyText = captureNodeSeq(body)
63    val href = "txmt://open?url=file://" + file +
64      (if (line.isDefined) "&line=" + line.get else "") +
65      (if (col.isDefined) "&col=" + col.get else "")
66
67    <a href={ href } title="Open in TextMate">
68      { bodyText }
69    </a>
70  }
71
72  def editLinkIdePlugin(file: String, line: Option[Int], col: Option[Int])(body: => Unit): NodeSeq = {
73    val bodyText = captureNodeSeq(body)
74
75    // The Atlassian IDE plugin seems to highlight the line after the actual line number, so lets subtract one
76    val lineNumber = if (line.isDefined) {
77      val n = line.get
78      if (n > 1) n - 1 else 0
79    } else 0
80
81    <span>
82      { bodyText }<img class="ide-icon tb_right_mid" id={ "ide-" + file.hashCode } title={ bodyText } onclick={ "this.src='http://localhost:" + idePluginPort + "/file?file=" + file + "&line=" + lineNumber + "&id=' + Math.floor(Math.random()*1000);" } alt="Open in IDE" src={ "http://localhost:" + idePluginPort + "/icon" }/>
83    </span>
84  }
85
86  def isMacOsx = System.getProperty("os.name", "").contains("Mac OS X")
87
88  def hasTextMate = exists("/Applications/TextMate.app") || exists("~/Applications/TextMate.app")
89
90  def exists(fileName: String) = new File(fileName).exists
91
92}