Source

pida-main / pida-plugins / todo / todo.py

Full commit
# -*- coding: utf-8 -*- 

# Copyright (c) 2007 The PIDA Project

from pygtkhelpers.gthreads import GeneratorTask, gcall
from pygtkhelpers.ui.objectlist import ObjectList, Column

# PIDA Imports
from pida.core.service import Service
from pida.core.features import FeaturesConfig
from pida.core.commands import CommandsConfig
from pida.core.events import EventsConfig
from pida.core.actions import ActionsConfig

from pida.ui.views import PidaView
from pida.ui.actions import PidaRememberToggle


# locale
from pida.core.locale import Locale
locale = Locale('todo')
_ = locale.gettext

class TodoItem(object):

    def __init__(self, todo, line, marker):
        self.todo = todo
        self.line = line
        self.marker = marker

class TodoView(PidaView):

    key = 'todo.list'

    label_text = _('TODO')
    icon_name =  'accessories-text-editor'

    def create_ui(self):
        self.todo_list = ObjectList([
            Column('line', sorted=True),
            Column('todo'),
            Column('marker'),
        ])
        self.add_main_widget(self.todo_list)
        self.todo_list.show_all()

    def clear_items(self):
        gcall(self.todo_list.clear)

    def add_item(self, todo, line, marker):
        self.todo_list.append(TodoItem(todo, line, marker))

    def on_todo_list__item_activated(self, olist, item):
        self.svc.boss.editor.cmd('goto_line', line=item.line)

    def can_be_closed(self):
        self.svc.get_action('show_todo').set_active(False)


#XXX Banana

class TodoActionsConfig(ActionsConfig):


    actions = [
        PidaRememberToggle('show_todo',
            _('Todo Viewer'),
            _('Show the Todo Viewer'),
            'accessories-text-editor'),
    ]

    accels = {
        'show_todo': '<Shift><Control>d',
    }

    def on_show_todo(self, action):
        if action.get_active():
            self.svc.show_todo()
        else:
            self.svc.hide_todo()

class TodoEventsConfig(EventsConfig):

    def subscribe_all_foreign(self):
        self.subscribe_foreign('buffer', 'document-changed',
                                     self.on_document_changed)
        self.subscribe_foreign('buffer', 'document-saved',
                                     self.on_document_changed)

    def on_document_changed(self, document):
        self.svc.set_current_document(document)


# Service class
class Todo(Service):
    """Describe your Service Here""" 

    actions_config = TodoActionsConfig
    events_config = TodoEventsConfig

    _markers = ['TODO', 'XXX', 'FIXME']

    def start(self):
        self._current = None
        self._view = TodoView(self)

    def show_todo(self):
        self.boss.cmd('window', 'add_view', paned='Plugin', view=self._view)

    def hide_todo(self):
        self.boss.cmd('window','remove_view', view=self._view)

    def check_current(self):
        for row in self.check_document(self._current):
            yield row

    def check_document(self, document):
        """Check the given lines for TODO messages."""
        self._view.clear_items()
        if not document or not hasattr(document, "lines") or document.lines:
            return
        for i, line in enumerate(document.lines):
            for marker in self._markers:
                if marker in line:
                    pre, post = line.split(marker, 1)
                    todo = post.strip().strip(':').strip()
                    yield (todo, i + 1, marker)

    def add_todo_item(self, todo, line, marker):
        self._view.add_item(todo, line, marker)

    def set_current_document(self, document):
        self._current = document
        if self._current is not None:
            task = GeneratorTask(self.check_current, self.add_todo_item)
            task.start()

    def stop(self):
        if self.get_action('show_todo').get_active():
            self.hide_todo()


# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: