Skip to content

Commit

Permalink
util: refactor "ble/gdict"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed May 27, 2021
1 parent c94d292 commit 7732eed
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 300 deletions.
2 changes: 1 addition & 1 deletion ble.pp
Expand Up @@ -494,7 +494,7 @@ function ble/base/read-blesh-arguments {

_ble_bash=$((BASH_VERSINFO[0]*10000+BASH_VERSINFO[1]*100+BASH_VERSINFO[2]))
_ble_bash_loaded_in_function=0
[[ ${FUNCNAME+set} ]] && _ble_bash_loaded_in_function=1
local _ble_local_test 2>/dev/null && _ble_bash_loaded_in_function=1

_ble_version=0
BLE_VERSION=$_ble_init_version
Expand Down
122 changes: 35 additions & 87 deletions lib/core-complete.sh
Expand Up @@ -6099,95 +6099,43 @@ function ble/complete/sabbrev/.print-definition {
## @param[in] key
## @var[out] ret
##
if ((_ble_bash>=40300||_ble_bash>=40000&&!_ble_bash_loaded_in_function)); then
function ble/complete/sabbrev/register {
local key=$1 value=$2
_ble_complete_sabbrev[$key]=$value
}
function ble/complete/sabbrev/list {
(($#)) || set -- "${!_ble_complete_sabbrev[@]}"
(($#)) || return 0

local sgr0 sgr1 sgr2 sgr3 sgro
ble/complete/sabbrev/.initialize-print

local key ext=0
for key; do
if [[ ${_ble_complete_sabbrev[$key]+set} ]]; then
local value=${_ble_complete_sabbrev[$key]}
ble/complete/sabbrev/.print-definition "$key" "$value"
else
ble/util/print "ble-sabbrev: $key: not found." >&2
ext=1
fi
done
return "$ext"
}
function ble/complete/sabbrev/get {
local key=$1
ret=${_ble_complete_sabbrev[$key]}
[[ $ret ]]
}
function ble/complete/sabbrev/get-keys {
keys=("${!_ble_complete_sabbrev[@]}")
}
else
if ! ble/is-array _ble_complete_sabbrev_keys; then # reload #D0875
_ble_complete_sabbrev_keys=()
_ble_complete_sabbrev_values=()
fi
function ble/complete/sabbrev/register {
local key=$1 value=$2 i=0
for key2 in "${_ble_complete_sabbrev_keys[@]}"; do
[[ $key2 == "$key" ]] && break
((i++))
done
_ble_complete_sabbrev_keys[i]=$key
_ble_complete_sabbrev_values[i]=$value
}
function ble/complete/sabbrev/list {
local sgr0 sgr1 sgr2 sgr3 sgro
ble/complete/sabbrev/.initialize-print

if (($#)); then
local key ret ext=0
for key; do
if ble/array#index _ble_complete_sabbrev_keys "$key"; then
local value=${_ble_complete_sabbrev_values[ret]}
ble/complete/sabbrev/.print-definition "$key" "$value"
else
ble/util/print "ble-sabbrev: $key: not found." >&2
ext=1
fi
done
return "$ext"

# Note: _ble_complete_sabbrev は core-complete-def.sh で定義
function ble/complete/sabbrev/register {
local key=$1 value=$2
ble/gdict#set _ble_complete_sabbrev "$key" "$value"
}
function ble/complete/sabbrev/list {
local keys ret; keys=("$@")
if ((${#keys[@]}==0)); then
ble/gdict#keys _ble_complete_sabbrev
keys=("${ret[@]}")
((${#keys[@]})) || return 0
fi

local sgr0 sgr1 sgr2 sgr3 sgro
ble/complete/sabbrev/.initialize-print

local key ext=0
for key in "${keys[@]}"; do
if ble/gdict#get _ble_complete_sabbrev "$key"; then
ble/complete/sabbrev/.print-definition "$key" "$ret"
else
local i N=${#_ble_complete_sabbrev_keys[@]}
for ((i=0;i<N;i++)); do
local key=${_ble_complete_sabbrev_keys[i]}
local value=${_ble_complete_sabbrev_values[i]}
ble/complete/sabbrev/.print-definition "$key" "$value"
done
return 0
ble/util/print "ble-sabbrev: $key: not found." >&2
ext=1
fi
}
function ble/complete/sabbrev/get {
ret=
local key=$1 value=$2 i=0
for key in "${_ble_complete_sabbrev_keys[@]}"; do
if [[ $key == "$1" ]]; then
ret=${_ble_complete_sabbrev_values[i]}
break
fi
((i++))
done
[[ $ret ]]
}
function ble/complete/sabbrev/get-keys {
keys=("${_ble_complete_sabbrev_keys[@]}")
}
fi

done
return "$ext"
}
function ble/complete/sabbrev/get {
local key=$1
ble/gdict#get _ble_complete_sabbrev "$key"
}
function ble/complete/sabbrev/get-keys {
local ret
ble/gdict#keys _ble_complete_sabbrev
keys=("${ret[@]}")
}

function ble/complete/sabbrev/read-arguments {
while (($#)); do
Expand Down
102 changes: 26 additions & 76 deletions lib/core-syntax.sh
Expand Up @@ -6287,50 +6287,26 @@ function ble/syntax/highlight/cmdtype/.impl {
## @param[in] word
## シェル展開・クォート除去を実行する前の文字列を指定します。
## @var[out] type
if ((_ble_bash>=40300||_ble_bash>=40000&&!_ble_bash_loaded_in_function)); then
# Note: 連想配列 _ble_syntax_highlight_filetype は core-syntax-def.sh で先に定義される。

_ble_syntax_highlight_filetype_version=-1
function ble/syntax/highlight/cmdtype {
local cmd=$1 _0=$2

# check cache
if ((_ble_syntax_highlight_filetype_version!=_ble_edit_LINENO)); then
_ble_syntax_highlight_filetype=()
((_ble_syntax_highlight_filetype_version=_ble_edit_LINENO))
fi

type=${_ble_syntax_highlight_filetype[x$_0]}
[[ $type ]] && return 0
# Note: 連想配列 _ble_syntax_highlight_filetype は core-syntax-def.sh で先に定義される。
_ble_syntax_highlight_filetype_version=-1
function ble/syntax/highlight/cmdtype {
local cmd=$1 _0=$2

ble/syntax/highlight/cmdtype/.impl "$cmd" "$_0"
_ble_syntax_highlight_filetype["x$_0"]=$type
}
else
_ble_syntax_highlight_filetype=()
_ble_syntax_highlight_filetype_version=-1
function ble/syntax/highlight/cmdtype {
local cmd=$1 _0=$2

# check cache
if ((_ble_syntax_highlight_filetype_version!=_ble_edit_LINENO)); then
_ble_syntax_highlight_filetype=()
((_ble_syntax_highlight_filetype_version=_ble_edit_LINENO))
fi
# check cache
if ((_ble_syntax_highlight_filetype_version!=_ble_edit_LINENO)); then
ble/gdict#clear _ble_syntax_highlight_filetype
((_ble_syntax_highlight_filetype_version=_ble_edit_LINENO))
fi

local i iN
for ((i=0,iN=${#_ble_syntax_highlight_filetype[@]}/2;i<iN;i++)); do
if [[ ${_ble_syntax_highlight_filetype[2*i]} == x"$_0" ]]; then
type=${_ble_syntax_highlight_filetype[2*i+1]}
return 0
fi
done
if local ret; ble/gdict#get _ble_syntax_highlight_filetype "$_0"; then
type=$ret
return 0
fi

ble/syntax/highlight/cmdtype/.impl "$cmd" "$_0"
_ble_syntax_highlight_filetype[2*iN]=x$_0
_ble_syntax_highlight_filetype[2*iN+1]=$type
}
fi
ble/syntax/highlight/cmdtype/.impl "$cmd" "$_0"
ble/gdict#set _ble_syntax_highlight_filetype "$_0" "$type"
}

#------------------------------------------------------------------------------
# ble/syntax/highlight/filetype
Expand Down Expand Up @@ -6392,49 +6368,23 @@ function ble/syntax/highlight/filetype {

function ble/syntax/highlight/ls_colors/.clear {
_ble_syntax_highlight_lscolors=()
_ble_syntax_highlight_lscolors_ext=()
ble/gdict#clear _ble_syntax_highlight_lscolors_ext
}

## @fn ble/syntax/highlight/ls_colors/.register-extension key value
## @param[in] key value
## @fn ble/syntax/highlight/ls_colors/.read-extension key
## @param[in] key
## @var[out] ret
if ((_ble_bash>=40300||_ble_bash>=40000&&!_ble_bash_loaded_in_function)); then
# 連想配列による実装
function ble/syntax/highlight/ls_colors/.register-extension {
local key=$1 value=$2
_ble_syntax_highlight_lscolors_ext[$key]=$value
}
function ble/syntax/highlight/ls_colors/.read-extension {
ret=${_ble_syntax_highlight_lscolors_ext[$1]}
[[ $ret ]]
}
else
# 通常配列による実装
## @fn ble/syntax/highlight/ls_colors/.find-extension key
## @var[out] ret
function ble/syntax/highlight/ls_colors/.find-extension {
local key=$1
local i N=${#_ble_syntax_highlight_lscolors_ext[@]}
for ((i=0;i<N;i+=2)); do
[[ ${_ble_syntax_highlight_lscolors_ext[i]} == "$key" ]] && break
done
ret=$i
}
function ble/syntax/highlight/ls_colors/.register-extension {
local key=$1 value=$2 ret
ble/syntax/highlight/ls_colors/.find-extension "$key"
_ble_syntax_highlight_lscolors_ext[ret]=$key
_ble_syntax_highlight_lscolors_ext[ret+1]=$value
}
function ble/syntax/highlight/ls_colors/.read-extension {
local key=$1
ble/syntax/highlight/ls_colors/.find-extension "$key"
ret=${_ble_syntax_highlight_lscolors_ext[ret+1]}
[[ $ret ]]
}
fi

# Note: _ble_syntax_highlight_lscolors_ext は core-syntax-def.sh で先に定義
function ble/syntax/highlight/ls_colors/.register-extension {
local key=$1 value=$2
ble/gdict#set _ble_syntax_highlight_lscolors_ext "$key" "$value"
}
function ble/syntax/highlight/ls_colors/.read-extension {
ble/gdict#get _ble_syntax_highlight_lscolors_ext "$1"
}

function ble/syntax/highlight/ls_colors/.parse {
ble/syntax/highlight/ls_colors/.clear
Expand Down
1 change: 1 addition & 0 deletions memo/ChangeLog.md
Expand Up @@ -40,6 +40,7 @@
- main (`ble-update`): fix help message (contributed by NoahGorny) 50288bf
- syntax: support undocumented `${a~}` and `${a~~}` `#D1561` 4df29a6
- lib: support `lib/vim-airline` (motivated by huresche) `#D1565` da1d0ff
- util (`ble/gdict`): refactor `#D1569` 0000000
- color: support new face setting function `ble-face` `#D1568` 0000000
- util (`bleopt`): support option `-r` and `-u` and wildcards in option names `#D1568` 0000000
- util (`blehook`): hide internal hooks by default and support option `-a` `#D1568` 0000000
Expand Down

0 comments on commit 7732eed

Please sign in to comment.