Source

ropemacs / ropemacs / interface.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
import rope.base.change
from Pymacs import lisp
from rope.base import libutils
from rope.contrib import codeassist, generate, autoimport

import ropemacs
from ropemacs import refactor, lisputils
from ropemacs.lisputils import (lispfunction, interactive,
                                prefixed, rawprefixed, lisphook)


class Ropemacs(object):

    def __init__(self):
        self.project = None
        self.old_content = None
        lisp(DEFVARS)
        #lisp['pymacs-forget-mutability'] = True

        self.global_keys = [
            ('o', lisp.rope_open_project),
            ('k', lisp.rope_close_project),
            ('u', lisp.rope_undo),
            ('r', lisp.rope_redo),
            ('f', lisp.rope_find_file),
            ('4 f', lisp.rope_find_file_other_window),
            ('c', lisp.rope_project_config),
            ('n m', lisp.rope_create_module),
            ('n p', lisp.rope_create_package),
            ('n f', lisp.rope_create_file),
            ('n d', lisp.rope_create_directory)]

        self.local_keys = [
            ('/', lisp.rope_code_assist),
            ('?', lisp.rope_lucky_assist),
            ('g', lisp.rope_goto_definition),
            ('d', lisp.rope_show_doc),
            ('f', lisp.rope_find_occurrences)]
        self.shortcuts = [
            ('M-/', lisp.rope_code_assist),
            ('M-?', lisp.rope_lucky_assist),
            ('C-c g', lisp.rope_goto_definition),
            ('C-c d', lisp.rope_show_doc),
            ('C-c f', lisp.rope_find_occurrences)]
        self.hooks = (
            (lisp.before_save_hook, lisp.rope_before_save_actions),
            (lisp.after_save_hook, lisp.rope_after_save_actions),
            (lisp.kill_emacs_hook, lisp.rope_exiting_actions),
            (lisp.python_mode_hook, lisp.rope_register_local_keys))
        self._prepare_refactorings()
        self.autoimport = None

    def init(self):
        """Initialize rope mode"""
        for hook, function in self.hooks:
            lisp.add_hook(hook, function)

        prefix = lisp.ropemacs_global_prefix.value()
        if prefix is not None:
            for key, callback in self.global_keys:
                lisp.global_set_key(self._key_sequence(prefix + ' ' + key),
                                    callback)

    def _prepare_refactorings(self):
        for name in dir(refactor):
            if not name.startswith('_') and name != 'Refactoring':
                attr = getattr(refactor, name)
                if isinstance(attr, type) and \
                   issubclass(attr, refactor.Refactoring):
                    @rawprefixed
                    def do_refactor(prefix, self=self, refactoring=attr):
                        initial_asking = prefix is None
                        refactoring(self).show(initial_asking=initial_asking)
                    name = self._refactoring_name(attr)
                    setattr(self, name, do_refactor)
                    name = 'rope-' + name.replace('_', '-')
                    if attr.key is not None:
                        self.local_keys.append((attr.key, lisp[name]))

    def _refactoring_name(self, refactoring):
        return refactor.refactoring_name(refactoring)

    def _key_sequence(self, sequence):
        result = []
        for key in sequence.split():
            if key.startswith('C-'):
                number = ord(key[-1].upper()) - ord('A') + 1
                result.append(chr(number))
            elif key.startswith('M-'):
                number = ord(key[-1].upper()) + 0x80
                result.append(chr(number))
            else:
                result.append(key)
        return ''.join(result)

    @lisphook
    def before_save_actions(self):
        if self.project is not None:
            if not self._is_python_file(lisp.buffer_file_name()):
                return
            resource = self._get_resource()
            if resource.exists():
                self.old_content = resource.read()
            else:
                self.old_content = ''

    @lisphook
    def after_save_actions(self):
        if self.project is not None and self.old_content is not None:
            libutils.report_change(self.project, lisp.buffer_file_name(),
                                   self.old_content)
            self.old_content = None

    @lisphook
    def register_local_keys(self):
        prefix = lisp.ropemacs_local_prefix.value()
        for key, callback in self.local_keys:
            if prefix is not None:
                key = prefix + ' ' + key
                lisp.local_set_key(self._key_sequence(key), callback)
        for key, callback in self.shortcuts:
            if lisp['ropemacs-enable-shortcuts'].value():
                lisp.local_set_key(self._key_sequence(key), callback)

    @lisphook
    def exiting_actions(self):
        if self.project is not None:
            self.close_project()

    @lispfunction
    def unload_hook(self):
        """Unload registered hooks"""
        for hook, function in self.hooks:
            lisp.remove_hook(hook, function)

    @interactive
    def open_project(self):
        root = lisputils.ask_directory('Rope project root folder: ')
        if self.project is not None:
            self.close_project()
        progress = lisputils.create_progress('Opening "%s" project' % root)
        self.project = rope.base.project.Project(root)
        if lisp['ropemacs-enable-autoimport'].value():
            self.autoimport = autoimport.AutoImport(self.project)
        progress.done()

    @interactive
    def close_project(self):
        if self.project is not None:
            progress = lisputils.create_progress('Closing "%s" project' %
                                                 self.project.address)
            self.project.close()
            self.project = None
            progress.done()

    @interactive
    def write_project(self):
        if self.project is not None:
            progress = lisputils.create_progress(
                'Writing "%s" project data to disk' % self.project.address)
            self.project.sync()
            progress.done()

    @interactive
    def undo(self):
        self._check_project()
        change = self.project.history.tobe_undone
        if change is None:
            lisputils.message('Nothing to undo!')
            return
        if lisp.y_or_n_p('Undo <%s>? ' % str(change)):
            def undo(handle):
                for changes in self.project.history.undo(task_handle=handle):
                    self._reload_buffers(changes, undo=True)
            lisputils.runtask(undo, 'Undo refactoring', interrupts=False)

    @interactive
    def redo(self):
        self._check_project()
        change = self.project.history.tobe_redone
        if change is None:
            lisputils.message('Nothing to redo!')
            return
        if lisp.y_or_n_p('Redo <%s>? ' % str(change)):
            def redo(handle):
                for changes in self.project.history.redo(task_handle=handle):
                    self._reload_buffers(changes)
            lisputils.runtask(redo, 'Redo refactoring', interrupts=False)

    def _get_region(self):
        offset1 = self._get_offset()
        lisp.exchange_point_and_mark()
        offset2 = self._get_offset()
        lisp.exchange_point_and_mark()
        return min(offset1, offset2), max(offset1, offset2)

    def _get_offset(self):
        return lisp.point() - 1

    def _get_text(self):
        end = lisp.buffer_size() + 1
        old_min = lisp.point_min()
        old_max = lisp.point_max()
        narrowed = (old_min != 1 or old_max != end)
        if narrowed:
            lisp.narrow_to_region(1, lisp.buffer_size() + 1)
        try:
            #result = lisp.buffer_string()
	    coding_name = self._find_file_coding()
            result = lisp('(encode-coding-string'
                          ' (buffer-string) buffer-file-coding-system)')
            if coding_name:
                if coding_name.endswith('dos'):
                    result = result.replace('\r', '')
                if coding_name.split('-')[-1] in ('dos', 'unix', 'mac'):
                    coding_name = coding_name[:coding_name.rindex('-')]
                if coding_name.split('-')[0] in ('mule', 'iso'):
                    coding_name = coding_name[coding_name.index('-') + 1:]
                try:
                    result = unicode(result, coding_name)
                except (LookupError, UnicodeDecodeError):
                    result = unicode(result, 'utf-8')
            return result
        finally:
            if narrowed:
                lisp.narrow_to_region(old_min, old_max)

    def _find_file_coding(self):
        if lisp.fboundp(lisp['coding-system-name']):
            coding = lisp('(coding-system-name'
                          ' buffer-file-coding-system)')
        else:
            coding = lisp['buffer-file-coding-system'].value()
        if isinstance(coding, str):
            return coding
        if coding is not None and hasattr(coding, 'text'):
            return coding.text

    @interactive
    def goto_definition(self):
        self._check_project()
        resource, offset = self._get_location()
        definition = codeassist.get_definition_location(
            self.project, self._get_text(), offset, resource)
        if tuple(definition) != (None, None):
            lisp.push_mark()
            self._goto_location(definition)

    @interactive
    def show_doc(self):
        self._check_project()
        resource, offset = self._get_location()
        docs = codeassist.get_doc(
            self.project, self._get_text(), offset, resource)
        buffer = lisputils.make_buffer('*rope-pydoc*', docs, empty_goto=False)
        lisp.local_set_key('q', lisp.bury_buffer)

    @interactive
    def find_occurrences(self):
        self._check_project()
        self._save_buffers()
        resource, offset = self._get_location()

        optionals = {
            'unsure': ropemacs.dialog.Data('Find uncertain occurrences: ',
                                           default='no', values=['yes', 'no']),
            'resources': ropemacs.dialog.Data('Files to search: ')}
        action, values = ropemacs.dialog.show_dialog(
            lisputils.askdata, ['search', 'cancel'], optionals=optionals)
        if action == 'search':
            unsure = values.get('unsure') == 'yes'
            resources = ropemacs.refactor._resources(self.project,
                                                     values.get('resources'))
            def calculate(handle):
                return codeassist.find_occurrences(
                    self.project, resource, offset,
                    unsure=unsure, resources=resources, task_handle=handle)
            result = lisputils.runtask(calculate, 'Find Occurrences')
            text = []
            for occurrence in result:
                line = '%s : %s' % (occurrence.resource.path, occurrence.offset)
                if occurrence.unsure:
                    line += ' ?'
                text.append(line)
            text = '\n'.join(text) + '\n'
            buffer = lisputils.make_buffer('*rope-occurrences*',
                                           text, switch=True)
            lisp.set_buffer(buffer)
            lisp.local_set_key('\r', lisp.rope_occurrences_goto_occurrence)
            lisp.local_set_key('q', lisp.rope_occurrences_quit)


    @interactive
    def occurrences_goto_occurrence(self):
        self._check_project()
        start = lisp.line_beginning_position()
        end = lisp.line_end_position()
        line = lisp.buffer_substring_no_properties(start, end)
        tokens = line.split()
        if tokens:
            resource = self.project.get_resource(tokens[0])
            offset = int(tokens[2])
            lisp.find_file_other_window(resource.real_path)
            lisp.goto_char(offset + 1)
            lisp.switch_to_buffer_other_window('*rope-occurrences*')

    @interactive
    def occurrences_quit(self):
        lisputils.hide_buffer('*rope-occurrences*')

    @rawprefixed
    def code_assist(self, prefix):
        starting_offset, names = self._calculate_proposals()
        if prefix is not None:
            arg = lisp.prefix_numeric_value(prefix)
            if arg == 0:
                arg = len(names)
            common_start = self._calculate_prefix(names[:arg])
            lisp.insert(common_start[self._get_offset() - starting_offset:])
        source = self._get_text()
        offset = self._get_offset()
        starting = source[starting_offset:offset]
        prompt = 'Completion for %s: ' % starting
        result = lisputils.ask_values(prompt, names,
                                      starting=starting, exact=None)
        self._apply_assist(result, starting_offset, offset)

    @rawprefixed
    def lucky_assist(self, prefix):
        starting_offset, names = self._calculate_proposals()
        source = self._get_text()
        offset = self._get_offset()
        starting = source[starting_offset:offset]
        selected = 0
        if prefix is not None:
            selected = lisp.prefix_numeric_value(prefix)
        if 0 <= selected < len(names):
            result = names[selected]
        else:
            lisputils.message('Not enough proposals!')
            return
        self._apply_assist(result, starting_offset, offset)

    def _apply_assist(self, assist, start, offset):
        if ' : ' in assist:
            name, module = assist.rsplit(' : ', 1)
            lisp.delete_region(start + 1, offset + 1)
            lisp.insert(name)
            self._insert_import(name, module)
        else:
            lisp.delete_region(start + 1, offset + 1)
            lisp.insert(assist)

    def _calculate_proposals(self):
        self._check_project()
        resource, offset = self._get_location()
        source = self._get_text()
        maxfixes = lisp['ropemacs-codeassist-maxfixes'].value()
        proposals = codeassist.code_assist(self.project, source, offset,
                                           resource, maxfixes=maxfixes)
        proposals = codeassist.sorted_proposals(proposals)
        starting_offset = codeassist.starting_offset(source, offset)
        names = [proposal.name for proposal in proposals]
        if self.autoimport is not None:
            starting = source[starting_offset:offset]
            names.extend(x[0] + ' : ' + x[1]
                         for x in self.autoimport.import_assist(starting))   
        return starting_offset, names

    def _check_autoimport(self):
        self._check_project()
        if self.autoimport is None:
            lisputils.message('autoimport is disabled; '
                              'see `ropemacs-enable-autoimport\' variable')
            return False
        return True

    @interactive
    def auto_import(self):
        if not self._check_autoimport():
            return
        name = lisp.current_word()
        modules = self.autoimport.get_modules(name)
        if modules:
            if len(modules) == 1:
                module = modules[0]
            else:
                module = lisputils.ask_values(
                    'Which module to import: ', modules)
            self._insert_import(name, module)
        else:
            lisputils.message('Global name %s not found!' % name)

    @interactive
    def generate_autoimport_cache(self):
        if not self._check_autoimport():
            return
        modules = lisp['ropemacs-autoimport-modules'].value()
        modnames = []
        if modules:
            for i in range(len(modules)):
                modname = modules[i]
                if not isinstance(modname, basestring):
                    modname = modname.value()
                modnames.append(modname)
        def generate(handle):
            self.autoimport.generate_cache(task_handle=handle)
            self.autoimport.generate_modules_cache(modules, task_handle=handle)
        lisputils.runtask(generate, 'Generate autoimport cache')

    def _insert_import(self, name, module):
        lineno = self.autoimport.find_insertion_line(self._get_text())
        current = lisp.point()
        lisp.goto_line(lineno)
        newimport = 'from %s import %s\n' % (module, name)
        lisp.insert(newimport)
        lisp.goto_char(current + len(newimport))

    def _calculate_prefix(self, names):
        if not names:
            return ''
        prefix = names[0]
        for name in names:
            common = 0
            for c1, c2 in zip(prefix, name):
                if c1 == c2:
                    common += 1
                else:
                    break
            prefix = prefix[:common]
        return prefix

    @interactive
    def find_file(self):
        file = self._base_find_file()
        if file is not None:
            lisp.find_file(file.real_path)

    @interactive
    def find_file_other_window(self):
        file = self._base_find_file()
        if file is not None:
            lisp.find_file_other_window(file.real_path)

    def _base_find_file(self):
        self._check_project()
        files = self.project.get_files()
        names = []
        for file in files:
            names.append('<'.join(reversed(file.path.split('/'))))
        result = lisputils.ask_values('Rope Find File: ', names, exact=True)
        if result is not None:
            path = '/'.join(reversed(result.split('<')))
            file = self.project.get_file(path)
            return file
        lisputils.message('No file selected')

    @interactive
    def project_config(self):
        self._check_project()
        if self.project.ropefolder is not None:
            config = self.project.ropefolder.get_child('config.py')
            lisp.find_file(config.real_path)
        else:
            lisputils.message('No rope project folder found')

    @interactive
    def create_module(self):
        def callback(sourcefolder, name):
            return generate.create_module(self.project, name, sourcefolder)
        self._create('module', callback)

    @interactive
    def create_package(self):
        def callback(sourcefolder, name):
            folder = generate.create_package(self.project, name, sourcefolder)
            return folder.get_child('__init__.py')
        self._create('package', callback)

    @interactive
    def create_file(self):
        def callback(parent, name):
            return parent.create_file(name)
        self._create('file', callback, 'parent')

    @interactive
    def create_directory(self):
        def callback(parent, name):
            parent.create_folder(name)
        self._create('directory', callback, 'parent')

    def _create(self, name, callback, parentname='source'):
        self._check_project()
        confs = {'name': ropemacs.dialog.Data(name.title() + ' name: ')}
        parentname = parentname + 'folder'
        optionals = {parentname: ropemacs.dialog.Data(
                parentname.title() + ' Folder: ',
                default=self.project.address, kind='directory')}
        action, values = ropemacs.dialog.show_dialog(
            lisputils.askdata, ['perform', 'cancel'], confs, optionals)
        if action == 'perform':
            parent = libutils.path_to_resource(
                self.project, values.get(parentname, self.project.address))
            resource = callback(parent, values['name'])
            if resource:
                lisp.find_file(resource.real_path)

    def _goto_location(self, location, readonly=False):
        if location[0]:
            resource = location[0]
            if resource.project == self.project:
                lisp.find_file(str(location[0].real_path))
            else:
                lisp.find_file_read_only(str(location[0].real_path))
        if location[1]:
            lisp.goto_line(location[1])

    def _get_location(self):
        resource = self._get_resource()
        offset = self._get_offset()
        return resource, offset

    def _get_resource(self, filename=None):
        if filename is None:
            filename = lisp.buffer_file_name()
        resource = libutils.path_to_resource(self.project, filename, 'file')
        return resource

    def _check_project(self):
        if self.project is None:
            self.open_project()
        else:
            self.project.validate(self.project.root)

    def _reload_buffers(self, changes, undo=False):
        self._reload_buffers_for_changes(
            changes.get_changed_resources(),
            self._get_moved_resources(changes, undo))

    def _reload_buffers_for_changes(self, changed_resources,
                                    moved_resources={}):
        for resource in changed_resources:
            buffer = lisp.find_buffer_visiting(str(resource.real_path))
            if buffer:
                if resource.exists():
                    lisp.set_buffer(buffer)
                    lisp.revert_buffer(False, True)
                elif resource in moved_resources:
                    new_resource = moved_resources[resource]
                    lisp.kill_buffer(buffer)
                    lisp.find_file(new_resource.real_path)

    def _get_moved_resources(self, changes, undo=False):
        result = {}
        if isinstance(changes, rope.base.change.ChangeSet):
            for change in changes.changes:
                result.update(self._get_moved_resources(change))
        if isinstance(changes, rope.base.change.MoveResource):
            result[changes.resource] = changes.new_resource
        if undo:
            return dict([(value, key) for key, value in result.items()])
        return result

    def _save_buffers(self, only_current=False):
        ask = lisp['ropemacs-confirm-saving'].value()
        initial = lisp.current_buffer()
        current_buffer = lisp.current_buffer()
        if only_current:
            buffers = [current_buffer]
        else:
            buffers = lisp.buffer_list()
        for buffer in buffers:
            filename = lisp.buffer_file_name(buffer)
            if filename:
                if self._is_python_file(filename) and \
                   lisp.buffer_modified_p(buffer):
                    if not ask or lisp.y_or_n_p('Save %s buffer?' % filename):
                        lisp.set_buffer(buffer)
                        lisp.save_buffer()
        lisp.set_buffer(initial)

    def _is_python_file(self, path):
        resource = self._get_resource(path)
        return (resource is not None and
                resource.project == self.project and
                self.project.pycore.is_python_file(resource))


DEFVARS = """\
(defgroup ropemacs nil
  "ropemacs, an emacs plugin for rope."
  :link '(url-link "http://rope.sourceforge.net/ropemacs.html")
  :prefix "rope-")

(defcustom ropemacs-confirm-saving t
  "Shows whether to confirm saving modified buffers before refactorings.

If non-nil, you have to confirm saving all modified
python files before refactorings; otherwise they are
saved automatically.")

(defcustom ropemacs-codeassist-maxfixes 1
  "The number of errors to fix before code-assist.

How many errors to fix, at most, when proposing code completions.")

(defcustom ropemacs-enable-autoimport 'nil
  "Specifies whether autoimport should be enabled.")
(defcustom ropemacs-autoimport-modules nil
  "The name of modules whose global names should be cached.

The `rope-generate-autoimport-cache' reads this list and fills its
cache.")

(make-obsolete-variable
  'rope-confirm-saving 'ropemacs-confirm-saving)
(make-obsolete-variable
  'rope-code-assist-max-fixes 'ropemacs-codeassist-maxfixes)

(defcustom ropemacs-local-prefix "C-c r"
  "The prefix for ropemacs refactorings.

Use nil to prevent binding keys.")

(defcustom ropemacs-global-prefix "C-x p"
  "The prefix for ropemacs project commands.

Use nil to prevent binding keys.")

(defcustom ropemacs-enable-shortcuts 't
  "Shows whether to bind ropemacs shortcuts keys.

If non-nil it binds:

================  ============================
Key               Command
================  ============================
M-/               rope-code-assist
C-c g             rope-goto-definition
C-c d             rope-show-doc
C-c f             rope-find-occurrences
M-?               rope-lucky-assist
================  ============================
")

(provide 'ropemacs)
"""
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.