"$kak_opt_static_words" push and pop operations for a large number of fields

SOLVED, 13 Feb 2021…Improvements welcome.

‘The good news is I’m trying to solve a problem. The bad news is I need someone smarter in the room.’

So here’s my problem.

Current “sand-boxed” implementation of kakoune-java.kak inhibits a users interaction due to the poor resolution of and operations to the current static word list.

Regular expressions query a users fully qualified import statements against 410 potential java packages that provide builtin constants. The operations are limited to a push and pop on the static word list.

The implementation file line length is 7866 and represents my best worst effort, hence the cry for help.

Static word list thoughts:

Observations:

  • The length of main list (base case) and size of insertion list, enables min and max sub-string positions.
  • Continuous operations on a single data structure requires multiple variables to coordinate the insertion positions.

Considerations:

  • Main list is a constant, operations of type push and pop begin from this Min sub-string position.
  • A running total is needed for the appended list size which will be our Max.
  • Substring(Min, Max) represents all push minus pop operations on the list.
  • Final operation on the Main list is the concatenation of the newly sized insertion list.

Conclusion:

  • Traversal and regular expression matches on the Main data structure is never preformed. Push and pop operations only occur on the sub-string.

Lessons learned:

  • Helper functions for stack size, addition (push) and (pop) subtraction confirmation.
:echo %sh{ printf %s "$kak_opt_static_words" | awk '{print NF}' }
:echo %sh{ printf %s "$kak_opt_org_xml_sax_helpers_namespacesupport" | awk '{print NF}' }

References:

Code Snippet:

hook global ModuleLoaded javaConstants %{
  require-module java
}

hook global WinSetOption filetype=java %{
  require-module javaConstants
  
  # REPEATED 410 TIMES #
  declare-option str org_xml_sax_helpers_namespacesupport_active "nop"

  hook -group java-constant-field window InsertIdle .* java-constants-add
  hook -group java-constant-field window ModeChange push:insert:.* java-constants-remove

  hook -once -always window WinSetOption filetype=.* %{ remove-hooks window java-constant-field }
}

provide-module javaConstants %§

  define-command java-constants-add %{
    # REPEATED 410 TIMES #
    try %{
      execute-keys -draft '%s (?:org.xml.sax.helpers.NamespaceSupport)<ret>'
      evaluate-commands %opt{org_xml_sax_helpers_namespacesupport_active}
      set-option -add window static_words %opt{org_xml_sax_helpers_namespacesupport}
      set-option window org_xml_sax_helpers_namespacesupport_active "nay"
    }
  }
  
  define-command java-constants-remove %{
    # REPEATED 410 TIMES #
    try %{
      execute-keys -draft '%s (?:org.xml.sax.helpers.NamespaceSupport)<ret>'
    } catch %{
      set-option -remove window static_words %opt{org_xml_sax_helpers_namespacesupport}
      set-option window org_xml_sax_helpers_namespacesupport_active "nop"
    }
  }
  
  # YEP, ALL BELOW LOGIC IS REPEATED 410 TIMES #
  evaluate-commands %sh{
    exec awk -f - <<'EOF'
    BEGIN { 
      split("NSDECL XMLNS", orgXmlSaxHelpersNamespaceSupport);
    }

    function print_static_words(words) { for (i in words) { printf(" %s", words[i]); } }

    BEGIN {
      printf("declare-option str-list org_xml_sax_helpers_namespacesupport ");
      print_static_words(orgXmlSaxHelpersNamespaceSupport);
      printf("\n");
    }
EOF
}
§

As always, thanks guys and bye :wave:.

Unless this issue has been fixed

Repeated adds and removes should generally be avoided. You’d be better off just setting the entire option once.

You could make the module more performant by calling the hook less, for example instead of on InsertIdle you could just check for InsertKey ‘;’ and then only run the update if the modified line was an import (and do similar for deleting).

Finally you could make it Async by forking the awk script and having it callback with the kak cli rather than directly to eval. Although I’d guess your performance issues are due to all the option modification and not the awk script.

Hey prion thanks for the info, bookmarked it cheers.

I have solved this problem and I’m onto the next one.

Thanks, prion.


Great tip, I went with
hook -group java-constant-field window InsertCompletionHide .* java-constants-add

as the completer adds the semicolon.
java.lang.Character;

Cool, bye.