src/Tools/jEdit/src/jedit_thy_load.scala
author wenzelm
Mon, 18 Nov 2013 19:56:34 +0100
changeset 54511 1fd24c96ce9b
parent 54509 1f77110c94ef
child 54515 570ba266f5b5
permissions -rw-r--r--
caching of blob; precise file content according to jEdit IO;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45007
cc86edb97c2c fixed headers;
wenzelm
parents: 44963
diff changeset
     1
/*  Title:      Tools/jEdit/src/jedit_thy_load.scala
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
     3
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
     4
Primitives for loading theory files, based on jEdit buffer content.
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
     5
*/
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
     6
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
     8
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
     9
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    10
import isabelle._
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    11
54511
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
    12
import java.io.{File => JFile, IOException, ByteArrayOutputStream}
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    13
import javax.swing.text.Segment
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    14
44963
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    15
import org.gjt.sp.jedit.io.{VFS, FileVFS, VFSFile, VFSManager}
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    16
import org.gjt.sp.jedit.MiscUtilities
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    17
import org.gjt.sp.jedit.{View, Buffer}
54511
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
    18
import org.gjt.sp.jedit.bufferio.BufferIORequest
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    19
48870
4accee106f0f clarified initialization of Thy_Load, Thy_Info, Session;
wenzelm
parents: 48422
diff changeset
    20
class JEdit_Thy_Load(loaded_theories: Set[String] = Set.empty, base_syntax: Outer_Syntax)
4accee106f0f clarified initialization of Thy_Load, Thy_Info, Session;
wenzelm
parents: 48422
diff changeset
    21
  extends Thy_Load(loaded_theories, base_syntax)
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    22
{
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    23
  /* document node names */
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    24
54509
1f77110c94ef maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents: 50566
diff changeset
    25
  def dummy_node_name(buffer: Buffer): Document.Node.Name =
1f77110c94ef maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents: 50566
diff changeset
    26
    Document.Node.Name(JEdit_Lib.buffer_name(buffer), buffer.getDirectory, buffer.getName)
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    27
54509
1f77110c94ef maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents: 50566
diff changeset
    28
  def node_name(buffer: Buffer): Document.Node.Name =
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    29
  {
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    30
    val name = JEdit_Lib.buffer_name(buffer)
54509
1f77110c94ef maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents: 50566
diff changeset
    31
    Document.Node.Name(name, buffer.getDirectory, Thy_Header.thy_name(name).getOrElse(""))
1f77110c94ef maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents: 50566
diff changeset
    32
  }
1f77110c94ef maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents: 50566
diff changeset
    33
1f77110c94ef maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents: 50566
diff changeset
    34
  def theory_node_name(buffer: Buffer): Option[Document.Node.Name] =
1f77110c94ef maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents: 50566
diff changeset
    35
  {
1f77110c94ef maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents: 50566
diff changeset
    36
    val name = node_name(buffer)
1f77110c94ef maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents: 50566
diff changeset
    37
    if (name.is_theory) Some(name) else None
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    38
  }
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    39
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    40
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    41
  /* file-system operations */
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    42
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44580
diff changeset
    43
  override def append(dir: String, source_path: Path): String =
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    44
  {
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    45
    val path = source_path.expand
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    46
    if (path.is_absolute) Isabelle_System.platform_path(path)
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    47
    else {
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44580
diff changeset
    48
      val vfs = VFSManager.getVFSForPath(dir)
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    49
      if (vfs.isInstanceOf[FileVFS])
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    50
        MiscUtilities.resolveSymlinks(
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44580
diff changeset
    51
          vfs.constructPath(dir, Isabelle_System.platform_path(path)))
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44580
diff changeset
    52
      else vfs.constructPath(dir, Isabelle_System.standard_path(path))
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    53
    }
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    54
  }
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    55
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    56
  override def with_thy_text[A](name: Document.Node.Name, f: CharSequence => A): A =
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    57
  {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    58
    Swing_Thread.now {
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents: 48885
diff changeset
    59
      JEdit_Lib.jedit_buffer(name.node) match {
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    60
        case Some(buffer) =>
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents: 48885
diff changeset
    61
          JEdit_Lib.buffer_lock(buffer) {
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    62
            Some(f(buffer.getSegment(0, buffer.getLength)))
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    63
          }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    64
        case None => None
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    65
      }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    66
    } getOrElse {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    67
      val file = new JFile(name.node)  // FIXME load URL via jEdit VFS (!?)
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    68
      if (!file.exists || !file.isFile) error("No such file: " + quote(file.toString))
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    69
      f(File.read(file))
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    70
    }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    71
  }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    72
44963
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    73
  def check_file(view: View, path: String): Boolean =
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    74
  {
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    75
    val vfs = VFSManager.getVFSForPath(path)
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    76
    val session = vfs.createVFSSession(path, view)
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    77
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    78
    try {
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    79
      session != null && {
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    80
        try {
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    81
          val file = vfs._getFile(session, path, view)
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    82
          file != null && file.isReadable && file.getType == VFSFile.FILE
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    83
        }
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    84
        catch { case _: IOException => false }
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    85
      }
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    86
    }
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    87
    finally {
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    88
      try { vfs._endVFSSession(session, view) }
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    89
      catch { case _: IOException => }
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    90
    }
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    91
  }
54511
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
    92
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
    93
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
    94
  /* file content */
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
    95
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
    96
  def file_content(buffer: Buffer): Bytes =
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
    97
  {
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
    98
    val path = buffer.getPath
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
    99
    val vfs = VFSManager.getVFSForPath(path)
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   100
    val content =
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   101
      new BufferIORequest(null, buffer, null, vfs, path) {
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   102
        def _run() { }
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   103
        def apply(): Bytes =
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   104
        {
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   105
          val out =
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   106
            new ByteArrayOutputStream(buffer.getLength + 1) {
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   107
              def content(): Bytes = Bytes(this.buf, 0, this.count)
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   108
            }
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   109
          write(buffer, out)
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   110
          out.content()
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   111
        }
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   112
      }
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   113
    content()
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   114
  }
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
   115
}
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
   116