#REFACTORING by exept
authorClaus Gittinger <cg@exept.de>
Wed, 14 Aug 2019 16:46:31 +0200
changeset 6667 af908df41a5e
parent 6666 7aa9f3c69ba2
child 6668 bba390644040
#REFACTORING by exept class: EditTextView added: #wordBeforeCursor #wordBeforeCursorConsisitingOfCharactersMatching:
EditTextView.st
--- a/EditTextView.st	Wed Aug 14 16:45:59 2019 +0200
+++ b/EditTextView.st	Wed Aug 14 16:46:31 2019 +0200
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
               All Rights Reserved
@@ -1654,6 +1656,38 @@
     typeOfSelection := selType.
 
     "Created: / 31.3.1998 / 23:35:06 / cg"
+!
+
+wordBeforeCursor
+    "return the word to the left of cursor - empty string if there is none"
+
+    ^ self wordBeforeCursorConsisitingOfCharactersMatching:#isAlphaNumeric
+!
+
+wordBeforeCursorConsisitingOfCharactersMatching:characterMatchBlock
+    "return the word to the left of cursor, where characters match the given condition;
+     empty string if there is none"
+
+    |  currentLine wordStart wordEnd |
+
+    currentLine := self list at: self cursorLine ifAbsent:[ ^ '' ].
+    currentLine isNil ifTrue:[ ^ '' ].
+    wordEnd := self cursorCol - 1.
+    wordEnd > currentLine size ifTrue:[ ^ '' ].
+    wordEnd ~~ 0 ifTrue:[
+        wordStart := wordEnd.
+        [ wordStart > 0 and:[characterMatchBlock value:(currentLine at: wordStart) ] ] whileTrue:[
+            wordStart := wordStart - 1.
+        ].
+        wordStart := wordStart + 1.
+        wordStart <= wordEnd ifTrue:[
+            ^ currentLine copyFrom: wordStart to: wordEnd.
+        ].
+    ].
+    ^ ''
+
+    "Created: / 31-03-2014 / 23:02:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 17-06-2014 / 07:27:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !EditTextView methodsFor:'accessing-dimensions'!