Skip to content

Open a method source in the editor of your choice #768

@nbruin

Description

@nbruin

It is easy look up the location of the definition of a method or function in sage, but opening the relevant file in an editor requires cutting and pasting. The following code should do that for you:

import sage.misc.sageinspect
import sage.misc.sagedoc
import inspect

from string import Template

defaulttemplate = Template('nedit-nc -line ${line} ${file}')

def fileandline(obj):
   r"""
   Look up source file and line number of obj
   """
   d = inspect.getdoc(obj)
   ret = sage.misc.sageinspect._extract_embedded_position(d);
   if ret is not None:
     (_, filename, lineno) = ret
   else:
     filename = inspect.getsourcefile(obj)
     _,lineno = inspect.findsource(obj)
#      
#  for sage files, the registered source file is the result of the preparsing
#  these files end in ".py" and have "*autogenerated*" on the second line
#  for those files, we replace the extension by ".sage" and we subtract
#  3 from the line number to compensate for the 3 lines that were prefixed
#  in the preparsing process
#
     if filename[-3:] == '.py':
       infile=open(filename,'r')
       infile.readline()
       if infile.readline().find("*autogenerated*") >=0:
         filename=filename[:-3]+'.sage'
         lineno = lineno-3
 
   return filename, lineno
   
def edit(obj,template=defaulttemplate):
   """
   Open source of obj in editor of your choice
   """
   filename,lineno = fileandline(obj)
   os.system(edittemplate.substitute(line=lineno,file=filename))

Before this code gets included there are some issues to be addressed:

  • The default template should be settable in some way. What would be the appropriate way of doing that? Can there be a static variable in the module that gets set by a routine "seteditortemplate" or something like that?
  • I have been told that the file returned for library code will be in the running version, which will get overwritten upon the next sage -br. Can we change the file name in the appropriate way?
  • no error handling is in place
  • Where should it go?

Component: user interface

Issue created by migration from https://trac.sagemath.org/ticket/768

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy