""" Experimental commands that may be removed from Wing in the future.

See the Scripting chapter of the manual for more information on writing 
and using scripts.

Copyright (c) 2005-2015, Wingware All rights reserved.
Copyright (c) 2005, Ken Kinder All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""

import wingapi

class _SimpleNamesCallbackSupport:
    """ Support for a simple name callback for the autocompleter """

    def __init__(self):

        self._callbacks = {}
        self._editor_open_conn = None

    def set_callback(self, mtype, callback):
        """ Set a callback for a mime type -- use None to clear one. """

        def forward(ed, ctx):
            return callback(wingapi.CAPIEditor._GetWrapper(ed), ctx)

        if callback is None:
            self._callbacks.pop(mtype, None)
        else:
            self._callbacks[mtype] = forward

        if len(self._callbacks) != 0 and self._editor_open_conn is None:
            self._editor_open_conn = wingapi.gApplication.Connect(
                'editor-open', self._editor_opened_callback)
        elif len(self._callbacks) == 0 and self._editor_open_conn is not None:
            wingapi.gApplication.Disconnect(self._editor_open_conn)
            self._editor_open_conn = None

    def _editor_opened_callback(self, ed):
        """ Signal handlerr invoked when editor is opened to set callback
        on internal object """

        ac = ed.fEditor._fAutocompleter
        if not isinstance(ac, wingapi.edit.cap_autocomplete.CDefaultAutocompleter):
            return

        mtype = ed.GetDocument().GetMimeType()
        callback = self._callbacks.get(mtype)
        if callback is None:
            return

        ac.SetSimpleNamesCallback(callback)

_gSimpleNamesCallbackSupport = _SimpleNamesCallbackSupport()
def experimental_install_simple_names_callback(mtype, callback):
    """ Install a callback for using a list of simple names for the autocomplete
    popup list for editors for the given mime type.  This an experimental feature
    and may change in future releases.  The callback needs to take 2 arguments:
    (internal-editor-object, ctx) and should return a list of strings to use
    for autocompletion.  The ctx object will have the following attributes:
    
    lineno -- line number of the line with the cursor
    cursor_pos -- position of the cursor in the document
    start_pos -- position of the word's first character in the document

    Currently this cannot be used for python documents

    An example of using this from a script is::
    
      def _callback(ed, ctx):
        return ['one', 'two']
      wingapi.gApplication.ExecuteCommand('experimental_install_simple_names_callback',
                                          mtype='text/plain',
                                          callback=_callback)
                                          
    """

    if mtype == 'text/x-python':
        raise ValueError("Cannot set a callback for python files")

    _gSimpleNamesCallbackSupport.set_callback(mtype, callback)
