src/Tools/jEdit/src/spell_checker.scala
changeset 56576 86148ca3c4fd
parent 56574 2b38472a4695
child 56577 58d7960058f5
equal deleted inserted replaced
56575:cdd609ea595d 56576:86148ca3c4fd
     9 
     9 
    10 
    10 
    11 import isabelle._
    11 import isabelle._
    12 
    12 
    13 import java.lang.Class
    13 import java.lang.Class
       
    14 import java.awt.event.MouseEvent
       
    15 import javax.swing.JMenuItem
    14 
    16 
    15 import scala.collection.mutable
    17 import scala.collection.mutable
    16 import scala.swing.ComboBox
    18 import scala.swing.ComboBox
    17 import scala.annotation.tailrec
    19 import scala.annotation.tailrec
    18 import scala.collection.immutable.SortedMap
    20 import scala.collection.immutable.SortedMap
    19 
    21 
    20 import org.gjt.sp.jedit.textarea.TextArea
    22 import org.gjt.sp.jedit.jEdit
       
    23 import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
       
    24 import org.gjt.sp.jedit.menu.EnhancedMenuItem;
       
    25 import org.gjt.sp.jedit.gui.DynamicContextMenuService;
    21 
    26 
    22 
    27 
    23 object Spell_Checker
    28 object Spell_Checker
    24 {
    29 {
    25   /* words within text */
    30   /* words within text */
    55       }
    60       }
    56     }
    61     }
    57     result.toList
    62     result.toList
    58   }
    63   }
    59 
    64 
    60   def current_word(text_area: TextArea, rendering: Rendering): Option[Text.Info[String]] =
    65   def current_word(text_area: TextArea, rendering: Rendering, range: Text.Range)
    61   {
    66     : Option[Text.Info[String]] =
    62     val caret = JEdit_Lib.before_caret_range(text_area, rendering)
    67   {
    63     for {
    68     for {
    64       spell_range <- rendering.spell_checker_point(caret)
    69       spell_range <- rendering.spell_checker_point(range)
    65       text <- JEdit_Lib.try_get_text(text_area.getBuffer, spell_range)
    70       text <- JEdit_Lib.try_get_text(text_area.getBuffer, spell_range)
    66       info <- marked_words(spell_range.start, text, info => info.range.overlaps(caret)).headOption
    71       info <- marked_words(spell_range.start, text, info => info.range.overlaps(range)).headOption
    67     } yield info
    72     } yield info
    68   }
    73   }
    69 
    74 
    70 
    75 
    71   /* dictionary declarations */
    76   /* dictionary declarations */
   290       }
   295       }
   291     }
   296     }
   292     else current_spell_checker = no_spell_checker
   297     else current_spell_checker = no_spell_checker
   293   }
   298   }
   294 }
   299 }
       
   300 
       
   301 
       
   302 class Spell_Checker_Menu extends DynamicContextMenuService
       
   303 {
       
   304   def createMenu(text_area: JEditTextArea, evt: MouseEvent): Array[JMenuItem] =
       
   305   {
       
   306     if (evt != null && evt.getSource == text_area.getPainter) {
       
   307       val result =
       
   308         for {
       
   309           spell_checker <- PIDE.spell_checker.get
       
   310           doc_view <- PIDE.document_view(text_area)
       
   311           rendering = doc_view.get_rendering()
       
   312           offset = text_area.xyToOffset(evt.getX, evt.getY)
       
   313           if offset >= 0
       
   314           range = JEdit_Lib.point_range(text_area.getBuffer, offset)
       
   315           Text.Info(_, word) <- Spell_Checker.current_word(text_area, rendering, range)
       
   316         } yield spell_checker.check(word)
       
   317 
       
   318       result match {
       
   319         case Some(known_word) =>
       
   320           val context = jEdit.getActionContext()
       
   321           def item(action: String) =
       
   322             new EnhancedMenuItem(context.getAction(action).getLabel, action, context)
       
   323 
       
   324           if (known_word)
       
   325             Array(item("isabelle.exclude-word"), item("isabelle.exclude-word-permanently"))
       
   326           else
       
   327             Array(item("isabelle.include-word"), item("isabelle.include-word-permanently"))
       
   328 
       
   329         case None => null
       
   330       }
       
   331     }
       
   332     else null
       
   333   }
       
   334 }