Permalink
Browse files

Only discard a source file if the project is still open.

When a project is closed, buffers are closed after the 
project has been closed, and calling discardFile will 
re-instantiate a presentation compiler just to remove
a non-existing unit. Should bring down memory footprint.
  • Loading branch information...
1 parent 1deb48b commit b68194ed554088f379dc6d5018ed36179d3a5e73 @dragos dragos committed Apr 8, 2011
Showing with 69 additions and 69 deletions.
  1. +69 −69 org.scala-ide.sdt.core/src/scala/tools/eclipse/ScalaPlugin.scala
@@ -25,28 +25,28 @@ import org.eclipse.ui.plugin.AbstractUIPlugin
import org.osgi.framework.BundleContext
import scala.tools.eclipse.javaelements.{ ScalaElement, ScalaSourceFile }
-import scala.tools.eclipse.util.OSGiUtils.pathInBundle
+import scala.tools.eclipse.util.OSGiUtils.pathInBundle
import scala.tools.eclipse.templates.ScalaTemplateManager
-object ScalaPlugin {
+object ScalaPlugin {
var plugin: ScalaPlugin = _
-
+
/** Returns the active workbench shell, or null if one does not exist */
def getShell: Shell = {
val workbench = PlatformUI.getWorkbench
- var window =
+ var window =
Option(workbench.getActiveWorkbenchWindow) orElse workbench.getWorkbenchWindows.headOption
window.map { _.getShell }.orNull
}
}
class ScalaPlugin extends AbstractUIPlugin with IResourceChangeListener with IElementChangedListener with IPartListener {
ScalaPlugin.plugin = this
-
+
def pluginId = "org.scala-ide.sdt.core"
def compilerPluginId = "org.scala-ide.scala.compiler"
def libraryPluginId = "org.scala-ide.scala.library"
-
+
def wizardPath = pluginId + ".wizards"
def wizardId(name: String) = wizardPath + ".new" + name
def classWizId = wizardId("Class")
@@ -55,67 +55,67 @@ class ScalaPlugin extends AbstractUIPlugin with IResourceChangeListener with IEl
def applicationWizId = wizardId("Application")
def projectWizId = wizardId("Project")
def netProjectWizId = wizardId("NetProject")
-
+
def editorId = "scala.tools.eclipse.ScalaSourceFileEditor"
def builderId = pluginId + ".scalabuilder"
- def natureId = pluginId + ".scalanature"
+ def natureId = pluginId + ".scalanature"
def launchId = "org.scala-ide.sdt.launching"
val scalaCompiler = "SCALA_COMPILER_CONTAINER"
val scalaLib = "SCALA_CONTAINER"
- def scalaCompilerId = launchId + "." + scalaCompiler
- def scalaLibId = launchId + "." + scalaLib
+ def scalaCompilerId = launchId + "." + scalaCompiler
+ def scalaLibId = launchId + "." + scalaLib
def launchTypeId = "scala.application"
def problemMarkerId = pluginId + ".problem"
-
+
// Retained for backwards compatibility
val oldPluginId = "ch.epfl.lamp.sdt.core"
val oldLibraryPluginId = "scala.library"
val oldNatureId = oldPluginId + ".scalanature"
val oldBuilderId = oldPluginId + ".scalabuilder"
val oldLaunchId = "ch.epfl.lamp.sdt.launching"
- val oldScalaLibId = oldLaunchId + "." + scalaLib
-
+ val oldScalaLibId = oldLaunchId + "." + scalaLib
+
val scalaFileExtn = ".scala"
val javaFileExtn = ".java"
val jarFileExtn = ".jar"
-
+
val scalaCompilerBundle = Platform.getBundle(ScalaPlugin.plugin.compilerPluginId)
val compilerClasses = pathInBundle(scalaCompilerBundle, "/lib/scala-compiler.jar")
val continuationsClasses = pathInBundle(scalaCompilerBundle, "/lib/continuations.jar")
val compilerSources = pathInBundle(scalaCompilerBundle, "/lib/scala-compiler-src.jar")
-
+
val scalaLibBundle = Platform.getBundle(ScalaPlugin.plugin.libraryPluginId)
- val libClasses = pathInBundle(scalaLibBundle, "/lib/scala-library.jar")
- val libSources = pathInBundle(scalaLibBundle, "/lib/scala-library-src.jar")
+ val libClasses = pathInBundle(scalaLibBundle, "/lib/scala-library.jar")
+ val libSources = pathInBundle(scalaLibBundle, "/lib/scala-library-src.jar")
val dbcClasses = pathInBundle(scalaLibBundle, "/lib/scala-dbc.jar")
- val dbcSources = pathInBundle(scalaLibBundle, "/lib/scala-dbc-src.jar")
- val swingClasses = pathInBundle(scalaLibBundle, "/lib/scala-swing.jar")
- val swingSources = pathInBundle(scalaLibBundle, "/lib/scala-swing-src.jar")
+ val dbcSources = pathInBundle(scalaLibBundle, "/lib/scala-dbc-src.jar")
+ val swingClasses = pathInBundle(scalaLibBundle, "/lib/scala-swing.jar")
+ val swingSources = pathInBundle(scalaLibBundle, "/lib/scala-swing-src.jar")
lazy val templateManager = new ScalaTemplateManager()
-
+
val pageListener = new IPageListener {
- def pageOpened(page: IWorkbenchPage) {
+ def pageOpened(page: IWorkbenchPage) {
page.addPartListener(ScalaPlugin.this)
- }
-
- def pageClosed(page: IWorkbenchPage) {
- page.removePartListener(ScalaPlugin.this)
- }
-
- def pageActivated(page: IWorkbenchPage) {}
+ }
+
+ def pageClosed(page: IWorkbenchPage) {
+ page.removePartListener(ScalaPlugin.this)
+ }
+
+ def pageActivated(page: IWorkbenchPage) {}
}
-
+
private val projects = new HashMap[IProject, ScalaProject]
-
+
override def start(context: BundleContext) = {
super.start(context)
-
+
ResourcesPlugin.getWorkspace.addResourceChangeListener(this, IResourceChangeEvent.PRE_CLOSE | IResourceChangeEvent.POST_CHANGE)
JavaCore.addElementChangedListener(this)
PlatformUI.getWorkbench.getEditorRegistry.setDefaultEditor("*.scala", editorId)
PlatformUI.getWorkbench.getActiveWorkbenchWindow.addPageListener(pageListener)
-
+
println("Scala compiler bundle: " + scalaCompilerBundle.getLocation)
PerspectiveFactory.updatePerspective
diagnostic.StartupDiagnostics.run
@@ -126,10 +126,10 @@ class ScalaPlugin extends AbstractUIPlugin with IResourceChangeListener with IEl
Option(PlatformUI.getWorkbench.getActiveWorkbenchWindow).map(_.removePageListener(pageListener))
super.stop(context)
}
-
+
def workspaceRoot = ResourcesPlugin.getWorkspace.getRoot
-
- def getJavaProject(project: IProject) = JavaCore.create(project)
+
+ def getJavaProject(project: IProject) = JavaCore.create(project)
def getScalaProject(project: IProject): ScalaProject = projects.synchronized {
projects.get(project) match {
@@ -140,15 +140,15 @@ class ScalaPlugin extends AbstractUIPlugin with IResourceChangeListener with IEl
scalaProject
}
}
-
+
def getScalaProject(input: IEditorInput): ScalaProject = input match {
case fei: IFileEditorInput => getScalaProject(fei.getFile.getProject)
case cfei: IClassFileEditorInput => getScalaProject(cfei.getClassFile.getJavaProject.getProject)
case _ => null
}
def isScalaProject(project: IJavaProject): Boolean = isScalaProject(project.getProject)
-
+
def isScalaProject(project: IProject): Boolean =
try {
project != null && project.isOpen && (project.hasNature(natureId) || project.hasNature(oldNatureId))
@@ -158,14 +158,14 @@ class ScalaPlugin extends AbstractUIPlugin with IResourceChangeListener with IEl
override def resourceChanged(event: IResourceChangeEvent) {
(event.getResource, event.getType) match {
- case (project: IProject, IResourceChangeEvent.PRE_CLOSE) =>
- projects.synchronized {
+ case (project: IProject, IResourceChangeEvent.PRE_CLOSE) =>
+ projects.synchronized {
projects.get(project) match {
case Some(scalaProject) =>
projects.remove(project)
println("resetting compilers for " + project.getName)
scalaProject.resetCompilers
- case None =>
+ case None =>
}
}
case _ =>
@@ -175,51 +175,50 @@ class ScalaPlugin extends AbstractUIPlugin with IResourceChangeListener with IEl
override def elementChanged(event: ElementChangedEvent) {
findRemovedSources(event.getDelta)
}
-
- private def findRemovedSources(delta: IJavaElementDelta) {
- import IJavaElement._
+
+ private def findRemovedSources(delta: IJavaElementDelta) {
+ import IJavaElement._
import IJavaElementDelta._
-
+
val isChanged = delta.getKind == CHANGED
val isRemoved = delta.getKind == REMOVED
def hasFlag(flag: Int) = (delta.getFlags & flag) != 0
-
- val elem = delta.getElement
+
+ val elem = delta.getElement
val processChildren: Boolean = elem.getElementType match {
case JAVA_MODEL => true
case JAVA_PROJECT if !isRemoved && !hasFlag(F_CLOSED) => true
-
+
case PACKAGE_FRAGMENT_ROOT =>
if (isRemoved || hasFlag(F_REMOVED_FROM_CLASSPATH | F_ADDED_TO_CLASSPATH | F_ARCHIVE_CONTENT_CHANGED)) {
println("package fragment root changed (resetting pres compiler): " + elem)
getScalaProject(elem.getJavaProject.getProject).resetPresentationCompiler
false
- }
- else true
-
- case PACKAGE_FRAGMENT => true
-
+ } else true
+
+ case PACKAGE_FRAGMENT => true
+
case COMPILATION_UNIT if elem.isInstanceOf[ScalaSourceFile] && isRemoved =>
val project = elem.getJavaProject.getProject
if (project.isOpen) {
getScalaProject(project).
- doWithPresentationCompiler { _.discardSourceFile(elem.asInstanceOf[ScalaSourceFile]) }
+ doWithPresentationCompiler { _.discardSourceFile(elem.asInstanceOf[ScalaSourceFile]) }
}
false
-
+
case _ => false
}
-
+
if (processChildren)
delta.getAffectedChildren foreach { findRemovedSources(_) }
}
def logWarning(msg: String): Unit = getLog.log(new Status(IStatus.WARNING, pluginId, msg))
def logError(t: Throwable): Unit = logError(t.getClass + ":" + t.getMessage, t)
-
+
def logError(msg: String, t: Throwable): Unit = {
- val t1 = if (t != null) t else { val ex = new Exception ; ex.fillInStackTrace ; ex }
+ val t1 = if (t != null) t else { val ex = new Exception; ex.fillInStackTrace; ex }
val status1 = new Status(IStatus.ERROR, pluginId, IStatus.ERROR, msg, t1)
getLog.log(status1)
@@ -233,9 +232,9 @@ class ScalaPlugin extends AbstractUIPlugin with IResourceChangeListener with IEl
case _ =>
}
}
-
+
def bundlePath = check {
- val bundle = getBundle
+ val bundle = getBundle
val bpath = bundle.getEntry("/")
val rpath = FileLocator.resolve(bpath)
rpath.getPath
@@ -257,11 +256,11 @@ class ScalaPlugin extends AbstractUIPlugin with IResourceChangeListener with IEl
case e: Throwable =>
logError(msgIfError, e)
None
- }
+ }
}
-
+
def isBuildable(file: IFile) = (file.getName.endsWith(scalaFileExtn) || file.getName.endsWith(javaFileExtn))
-
+
// IPartListener
def partActivated(part: IWorkbenchPart) {}
def partDeactivated(part: IWorkbenchPart) {}
@@ -276,22 +275,23 @@ class ScalaPlugin extends AbstractUIPlugin with IResourceChangeListener with IEl
compiler.discardSourceFile(ssf)
}
}
-
- private def doWithCompilerAndFile(part : IWorkbenchPart)(op: (ScalaPresentationCompiler, ScalaSourceFile) => Unit) {
- part match {
+
+ private def doWithCompilerAndFile(part: IWorkbenchPart)(op: (ScalaPresentationCompiler, ScalaSourceFile) => Unit) {
+ part match {
case editor: IEditorPart =>
editor.getEditorInput match {
case fei: FileEditorInput =>
val f = fei.getFile
if (f.getName.endsWith(scalaFileExtn)) {
for (ssf <- ScalaSourceFile.createFromPath(f.getFullPath.toString)) {
- val proj = getScalaProject(f.getProject)
- proj.doWithPresentationCompiler(op(_, ssf)) // so that an exception is not thrown
+ val proj = getScalaProject(f.getProject)
+ if (proj.underlying.isOpen)
+ proj.doWithPresentationCompiler(op(_, ssf)) // so that an exception is not thrown
}
}
case _ =>
}
case _ =>
- }
+ }
}
}

0 comments on commit b68194e

Please sign in to comment.