Skip to content

Instantly share code, notes, and snippets.

@laurentpetit
Created March 1, 2012 18:10
Show Gist options
  • Save laurentpetit/1951764 to your computer and use it in GitHub Desktop.
Save laurentpetit/1951764 to your computer and use it in GitHub Desktop.
(ns ^{:doc "Eclipse interop utilities"}
ccw.util.eclipse
(:require [clojure.java.io :as io])
(:import [org.eclipse.core.resources IResource
ResourcesPlugin]
[org.eclipse.core.runtime IPath
Path]
[org.eclipse.jdt.core IJavaProject]))
(extend-protocol io/Coercions
IResource
(io/as-file [r] (io/as-file (.getLocation r)))
(io/as-url [r] (io/as-url (io/as-file r)))
IPath
(io/as-file [p] (.toFile p))
(io/as-url [p] (io/as-url (io/as-file p))))
(defn workspace
"Return the Eclipse Workspace" []
(ResourcesPlugin/getWorkspace))
(defn workspace-root
"Return the Eclipse Workspace root"
[]
(.getRoot (workspace)))
(defprotocol IProjectCoercion
(project [this] "Coerce this into an IProject"))
(defprotocol IResourceCoercion
(resource [this] "Coerce this in a IResource"))
(defprotocol IPathCoercion
(path [this] "Coerce this to an IPath"))
(extend-protocol IProjectCoercion
nil
(project [_] nil)
String
(project [s] (.getProject (workspace-root) s))
IResource
(project [r] (.getProject r)))
(extend-protocol IResourceCoercion
nil
(resource [this] nil)
IResource
(resource [this] this)
IJavaProject
(resource [this] (.getProject this))
String
(resource [filesystem-path] (resource (path filesystem-path)))
IPath
(resource [filesystem-path] (.getFileForLocation (workspace-root) filesystem-path)))
(extend-protocol IPathCoercion
nil
(path [_] nil)
IPath
(path [this] this)
String
(path [s] (Path. s)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment