Исправлены процедуры comment/uncomment

This commit is contained in:
svkalinin 2022-08-12 15:24:39 +03:00
parent d2c8cd40c6
commit b4230a8c46
3 changed files with 164 additions and 16 deletions

View File

@ -55,3 +55,9 @@
- Fix finded procedure (function) (tree click) - Fix finded procedure (function) (tree click)
- Fix showing position in statusbar - Fix showing position in statusbar
12/08/2022
- Fixed comment/uncomment procedure depending on the file type
- Added About dialog
- Fixed read structure (procedure names like Proc:Name)

View File

@ -9,11 +9,25 @@
namespace eval Editor { namespace eval Editor {
variable selectionTex variable selectionTex
proc Comment {txt} { proc Comment {txt fileType} {
set selIndex [$txt tag ranges sel] set selIndex [$txt tag ranges sel]
set pos [$txt index insert] set pos [$txt index insert]
set lineNum [lindex [split $pos "."] 0] set lineNum [lindex [split $pos "."] 0]
set PosNum [lindex [split $pos "."] 1] set PosNum [lindex [split $pos "."] 1]
switch $fileType {
TCL {
set symbol "#"
}
GO {
set symbol "//"
}
Unknown {
set symbol "#"
}
default {
set symbol "#"
}
}
puts "Select : $selIndex" puts "Select : $selIndex"
if {$selIndex != ""} { if {$selIndex != ""} {
set lineBegin [lindex [split [lindex $selIndex 0] "."] 0] set lineBegin [lindex [split [lindex $selIndex 0] "."] 0]
@ -26,22 +40,29 @@ namespace eval Editor {
for {set i $lineBegin} {$i <=$lineEnd} {incr i} { for {set i $lineBegin} {$i <=$lineEnd} {incr i} {
#$txt insert $i.0 "# " #$txt insert $i.0 "# "
regexp -nocase -indices -- {^(\s*)(.*?)} [$txt get $i.0 $i.end] match v1 v2 regexp -nocase -indices -- {^(\s*)(.*?)} [$txt get $i.0 $i.end] match v1 v2
$txt insert $i.[lindex [split $v2] 0] "# " $txt insert $i.[lindex [split $v2] 0] "$symbol "
} }
$txt tag add comments $lineBegin.0 $lineEnd.end $txt tag add comments $lineBegin.0 $lineEnd.end
$txt tag raise comments $txt tag raise comments
} else { } else {
regexp -nocase -indices -- {^(\s*)(.*?)} [$txt get $lineNum.0 $lineNum.end] match v1 v2 regexp -nocase -indices -- {^(\s*)(.*?)} [$txt get $lineNum.0 $lineNum.end] match v1 v2
$txt insert $lineNum.[lindex [split $v2] 0] "# " $txt insert $lineNum.[lindex [split $v2] 0] "$symbol "
$txt tag add comments $lineNum.0 $lineNum.end $txt tag add comments $lineNum.0 $lineNum.end
$txt tag raise comments $txt tag raise comments
} }
} }
proc Uncomment {txt} { proc Uncomment {txt fileType} {
set selIndex [$txt tag ranges sel] set selIndex [$txt tag ranges sel]
set pos [$txt index insert] set pos [$txt index insert]
set lineNum [lindex [split $pos "."] 0] set lineNum [lindex [split $pos "."] 0]
set posNum [lindex [split $pos "."] 1] set posNum [lindex [split $pos "."] 1]
if {[info procs GetComment:$fileType] ne ""} {
set commentProcedure "GetComment:$fileType"
} else {
set commentProcedure {GetComment:Unknown}
}
# puts "$fileType, $commentProcedure"
if {$selIndex != ""} { if {$selIndex != ""} {
set lineBegin [lindex [split [lindex $selIndex 0] "."] 0] set lineBegin [lindex [split [lindex $selIndex 0] "."] 0]
set lineEnd [lindex [split [lindex $selIndex 1] "."] 0] set lineEnd [lindex [split [lindex $selIndex 1] "."] 0]
@ -52,25 +73,48 @@ namespace eval Editor {
} }
for {set i $lineBegin} {$i <=$lineEnd} {incr i} { for {set i $lineBegin} {$i <=$lineEnd} {incr i} {
set str [$txt get $i.0 $i.end] set str [$txt get $i.0 $i.end]
if {[regexp -nocase -indices -- {(^| )(#\s)(.+)} $str match v1 v2 v3]} { set commentSymbolIndex [$commentProcedure $str]
$txt delete $i.[lindex [split $v2] 0] $i.[lindex [split $v3] 0] if {$commentSymbolIndex != 0} {
$txt delete $i.[lindex $commentSymbolIndex 0] $i.[lindex $commentSymbolIndex 1]
} }
} }
$txt tag remove comments $lineBegin.0 $lineEnd.end $txt tag remove comments $lineBegin.0 $lineEnd.end
$txt tag add sel $lineBegin.0 $lineEnd.end $txt tag add sel $lineBegin.0 $lineEnd.end
$txt highlight $lineBegin.0 $lineEnd.end $txt highlight $lineBegin.0 $lineEnd.end
} else { } else {
#set posNum [lindex [split $pos "."] 1] set posNum [lindex [split $pos "."] 1]
set str [$txt get $lineNum.0 $lineNum.end] set str [$txt get $lineNum.0 $lineNum.end]
if {[regexp -nocase -indices -- {(^| )(#\s)(.+)} $str match v1 v2 v3]} { set commentSymbolIndex [$commentProcedure $str]
puts ">>>>> $v1, $v2, $v3" if {$commentSymbolIndex != 0} {
$txt delete $lineNum.[lindex [split $v2] 0] $lineNum.[lindex [split $v3] 0] $txt delete $lineNum.[lindex $commentSymbolIndex 0] $lineNum.[lindex $commentSymbolIndex 1]
#$txt insert $i.0 $v3
} }
$txt tag remove comments $lineNum.0 $lineNum.end $txt tag remove comments $lineNum.0 $lineNum.end
$txt highlight $lineNum.0 $lineNum.end $txt highlight $lineNum.0 $lineNum.end
} }
} }
proc GetComment:TCL {str} {
if {[regexp -nocase -indices -- {(^| )(#\s)(.+)} $str match v1 v2 v3]} {
return [list [lindex [split $v2] 0] [lindex [split $v3] 0]]
} else {
return 0
}
}
proc GetComment:GO {str} {
# puts ">>>>>>>$str"
if {[regexp -nocase -indices -- {(^| |\t)(//\s)(.+)} $str match v1 v2 v3]} {
# puts ">>>> $match $v1 $v2 $v3"
return [list [lindex [split $v2] 0] [lindex [split $v3] 0]]
} else {
return 0
}
}
proc GetComment:Unknown {str} {
if {[regexp -nocase -indices -- {(^| )(#\s)(.+)} $str match v1 v2 v3]} {
return [list [lindex [split $v2] 0] [lindex [split $v3] 0]]
} else {
return 0
}
}
proc InsertTabular {txt} { proc InsertTabular {txt} {
global cfgVariables global cfgVariables
@ -302,7 +346,7 @@ namespace eval Editor {
if {$key == 63 || $key == 107 || $key == 108 || $key == 112} {return "true"} if {$key == 63 || $key == 107 || $key == 108 || $key == 112} {return "true"}
} }
proc BindKeys {w} { proc BindKeys {w fileType} {
global cfgVariables global cfgVariables
# variable txt # variable txt
set txt $w.frmText.t set txt $w.frmText.t
@ -341,8 +385,8 @@ namespace eval Editor {
bind $txt <Control-bracketleft> "Editor::InsertTabular $txt" bind $txt <Control-bracketleft> "Editor::InsertTabular $txt"
bind $txt <Control-bracketright> "Editor::DeleteTabular $txt" bind $txt <Control-bracketright> "Editor::DeleteTabular $txt"
bind $txt <Control-comma> "Editor::Comment $txt" bind $txt <Control-comma> "Editor::Comment $txt $fileType"
bind $txt <Control-period> "Editor::Uncomment $txt" bind $txt <Control-period> "Editor::Uncomment $txt $fileType"
bind $txt <Control-eacute> Find bind $txt <Control-eacute> Find
#bind . <Control-m> PageTab #bind . <Control-m> PageTab
#bind . <Control-udiaeresis> PageTab #bind . <Control-udiaeresis> PageTab
@ -414,7 +458,7 @@ namespace eval Editor {
for {set lineNumber 0} {$lineNumber <= [$txt count -lines 0.0 end]} {incr lineNumber} { for {set lineNumber 0} {$lineNumber <= [$txt count -lines 0.0 end]} {incr lineNumber} {
set line [$txt get $lineNumber.0 $lineNumber.end] set line [$txt get $lineNumber.0 $lineNumber.end]
# TCL procedure # TCL procedure
if {[regexp -nocase -all -- {^\s*?(proc) (::|)(\w+)(::|)(\w+)\s*?(\{|\()(.*)(\}|\)) \{} $line match v1 v2 v3 v4 v5 v6 params v8]} { if {[regexp -nocase -all -- {^\s*?(proc) (::|)(\w+)(::|:|)(\w+)\s*?(\{|\()(.*)(\}|\)) \{} $line match v1 v2 v3 v4 v5 v6 params v8]} {
set procName "$v2$v3$v4$v5" set procName "$v2$v3$v4$v5"
# lappend procList($activeProject) [list $procName [string trim $params]] # lappend procList($activeProject) [list $procName [string trim $params]]
# puts "$treeItemName proc $procName $params" # puts "$treeItemName proc $procName $params"
@ -496,6 +540,8 @@ namespace eval Editor {
$txt configure -linemap 0 $txt configure -linemap 0
} }
set fileType [string toupper [string trimleft [file extension $fileFullPath] "."]] set fileType [string toupper [string trimleft [file extension $fileFullPath] "."]]
if {$fileType eq ""} {set fileType "Unknown"}
# puts ">$fileType<" # puts ">$fileType<"
# puts [info procs Highlight::GO] # puts [info procs Highlight::GO]
if {[info procs ::Highlight::$fileType] ne ""} { if {[info procs ::Highlight::$fileType] ne ""} {
@ -504,7 +550,7 @@ namespace eval Editor {
Highlight::Default $txt Highlight::Default $txt
} }
BindKeys $itemName BindKeys $itemName $fileType
# bind $txt <Return> { # bind $txt <Return> {
# regexp {^(\s*)} [%W get "insert linestart" end] -> spaceStart # regexp {^(\s*)} [%W get "insert linestart" end] -> spaceStart
# %W insert insert "\n$spaceStart" # %W insert insert "\n$spaceStart"

96
lib/readstructure.tcl Normal file
View File

@ -0,0 +1,96 @@
######################################################
# ProjMan 2
# Distributed under GNU Public License
# Author: Sergey Kalinin svk@nuk-svk.ru
# Copyright (c) "", 2022, https://nuk-svk.ru
######################################################
#
# Module for read files structure into directory
#
######################################################
package require fileutil
package require Thread
# TCL procedure
proc ReadFileStructureTCL {fileFullName} {
global procList
set f [open "$fileFullName" r]
while {[gets $f line] >=0} {
if {[regexp -nocase -all -- {^\s*?(proc) (::|)(\w+)(::|:|)(\w+)\s*?(\{|\()(.*)(\}|\)) \{} $line match v1 v2 v3 v4 v5 v6 params v8]} {
set procName "$v2$v3$v4$v5"
lappend procList($fileFullName) [list $procName $params]
}
}
close $f
}
# GO function
proc ReadFileStructureGO {fileName} {
if {[regexp -nocase -all -- {^\s*?func\s*?\((\w+\s*?\*\w+)\)\s*?(\w+)\((.*?)\)\s*?(\(\w+\)|\w+|)\s*?\{} $line match v1 funcName params returns]} {
# set procName "$v2$v3$v4$v5"
# lappend procList($activeProject) [list $procName [string trim $params]]
if {$v1 ne ""} {
set linkName [lindex [split $v1 " "] 1]
set functionName "\($linkName\).$funcName"
}
# tree parent item type text
lappend procList($fuleFullName) [list $functionName $params]
}
if {[regexp -nocase -all -- {^\s*?func\s*?(\w+)\((.*?)\) (\(\w+\)|\w+|)\s*?\{} $line match funcName params returns]} {
lappend procList($fuleFullName) [list $functonName $params]
}
}
proc ReadFilesFromDirectory {directory} {
global procList
puts $directory
foreach fileName [fileutil::findByPattern $directory *.tcl] {
puts "Find file: $fileName"
ReadFileStructureTCL $fileName
}
set f [open "/tmp/test" w]
foreach name [array names procList] {
puts $f "$name: $procList($name)"
}
}
# set threadID [thread::create {
# proc runCommand {ID command} {
# set result [eval $command]
# eval [subst {thread::send -async $ID \
# {::printResult [list $result]}}]
# }
# thread::wait
# }]
#
proc Accept { dirLib directory } {
global dir
puts $dir(lib)
puts $dirLib
# переменная с указанием ваших действия перед порождением потока
set threadinit {
# если необходимо, загружаем исходный tcl код, расположенный в других файлах
foreach { s } { readstructure } {
# uplevel #0 source [file join /home/svkalinin/Проекты/projman/lib $s.tcl]
uplevel #0 source [file join $dirLib $s.tcl]
}
# не завершаем поток, ибо будет запущен событийный сокетный обработчик
thread::wait
}
# порождаем поток, выполнив предварительные действия, описанные в переменной threadinit
set tid [thread::create $threadinit]
# thread::transfer $tid
# запускаем поток в асинхронном режиме
thread::send -async $tid [list ReadFilesFromDirectory $directory]
}
# процедура завершения потока
proc Exit:Thread { } {
# уничтожаем, останавливаем поток
thread::release
}