Ejemplo n.º 1
0
def _GetCurrentASTPath(context, reverse=False):
    '''
    @return: ArrayList(SimpleNode)
    '''
    from org.python.pydev.parser.fastparser import FastParser
    selection = _CreateSelection(context)
    ret = FastParser.parseToKnowGloballyAccessiblePath(
        context.getDocument(), selection.getStartLineIndex())
    if reverse:
        from java.util import Collections
        Collections.reverse(ret)
        
    return ret
Ejemplo n.º 2
0
def _GetCurrentASTPath(context, reverse=False):
    '''
    @return: ArrayList(SimpleNode)
    '''
    FastParser = context.getFastParserClass()  # from org.python.pydev.parser.fastparser import FastParser
    selection = _CreateSelection(context)
    ret = FastParser.parseToKnowGloballyAccessiblePath(
        context.getDocument(), selection.getStartLineIndex())
    if reverse:
        from java.util import Collections  # @UnresolvedImport
        Collections.reverse(ret)

    return ret
    def extract_content_between_block_level_nodes(self, doc):
        """
            Shows how to extract the content between a paragraph and table using the ExtractContent method.
        """

        startPara = doc.getLastSection().getChild(NodeType.PARAGRAPH, 2, True)
        endTable = doc.getLastSection().getChild(NodeType.TABLE, 0, True)

        # Extract the content between these nodes in the document. Include these markers in the extraction.
        extractedNodes = self.extract_contents(startPara, endTable, True)

        # Lets reverse the array to make inserting the content back into the document easier.
        Collections.reverse(extractedNodes[::-1])

        while (len(extractedNodes) > 0):
            # Insert the last node from the reversed list
            endTable.getParentNode().insertAfter(extractedNodes[0], endTable)
            # Remove this node from the list after insertion.
            del extractedNodes[0]

        # Save the generated document to disk.
        doc.save(self.dataDir + "TestFile.DuplicatedContent Out.doc")
Ejemplo n.º 4
0
def GetMethodArg(idocument):
    '''
    This is an older version of the GetMethodArg2 function.  After 
    figuring out how to parse a document using the pydev parsers
    rewrote the method parsing functions.  This will get 
    deleted once I am comfortable that the the GetMethodArg2 is 
    working well.
    
    :param  idocument: an idocument object containing the entire
                       module that was created for this method.
    :type idocument: org.python.pydev.editor.codecompletion.templates.PyDocumentTemplateContext
    
    :returns: a formatted docstring template for the method on which
              this function was called
    :rtype: string
    '''
    # TODO: parse the method looking for the return type
    from org.python.pydev.parser.jython.ast import FunctionDef
    from org.python.pydev.parser.fastparser import FastParser
    from org.python.pydev.core.docutils import PySelection
    
    # 1 - get the current document
    doc = idocument.getDocument()
    # 2 - get a selection object for the current line
    selection = PySelection(idocument.getDocument(), idocument.getStart())
    startLineNumber = doc.getLineOfOffset(idocument.getStart())
    startLineContents = selection.getLine(startLineNumber)
    startIndentation = selection.getIndentationFromLine(startLineContents)
    print 'the line is', startLineNumber
    print 'contents is', startLineContents

    # 3 - create a parser object with the hierarchy of methods, classes etc to which the current line belongs
    parser = FastParser.parseToKnowGloballyAccessiblePath(
        idocument.getDocument(), selection.getStartLineIndex())
    if parser:
        # only continuing if the parser found a method definition. 
        print 'parser', parser
        print 'begin line', parser[0].beginLine
        print 'rettype', type(parser)
        print 'start is', idocument.getStart()
        from java.util import Collections
        Collections.reverse(parser)    
        # 4 - iterate through the parser object
        for stmt in parser:
            print 'stmt', stmt
            print type(stmt)
            # 5 - if the parser contains a method definition
            if isinstance(stmt, FunctionDef):
                # 6 - get the line number for the method definition
                print 'method line', stmt.beginLine
                
                functionDefinitionStmt = selection.getLine(stmt.beginLine-1)
                print 'defstatement:', functionDefinitionStmt
                print 'woke up'
                
                afterLine = stmt.beginLine - 1
#                listOfStringLines = []
                
                string2Add = prepOutputMethodString(startIndentation, functionDefinitionStmt)
    
                #selection.addLine( indentationString + string2Add, stmt.beginLine - 1)
                offset = doc.getLineInformation(afterLine + 1).getOffset();
                print 'offset 1 - ', offset
                if doc.getNumberOfLines() > stmt.beginLine - 1:
                    offset = doc.getLineInformation(afterLine + 1).getOffset();
                    print 'offset 2 - ', offset
                else:
                    offset = doc.getLineInformation(afterLine).getOffset()
                    print 'offset 2 - ', offset
                #doc.replace(offset, 0, string2Add)
                # inserting into the doc immediately after the method is
                # not working, instead will return the doc string
                return string2Add
                #selection.addLine( indentationString + string2Add, stmt.beginLine - 1)
                #offset = doc.getLineInformation(afterLine + 1).getOffset();
                #selection = PySelection(idocument.getDocument(), idocument.getStart())
                #offset = doc.getLineInformation(stmt.beginLine).getOffset()
                #print 'offset is now ',offset
                #print 'on line number', selection.getLineNumber()
                #args = NodeUtils.getNodeArgs(stmt)
                #print 'args are', args
                #print '.getRepresentationString' , NodeUtils.getRepresentationString(stmt)
                #print 'full rep string:', NodeUtils.getFullRepresentationString(stmt)
                #return NodeUtils.getRepresentationString(stmt)
    # if nothing was found then return null.
    return ""
Ejemplo n.º 5
0
def GetMethodArg2(idocument):
    '''
    This method will receive a PyDocumentTemplateContext.  From that 
    object it is able to determine the cursor position, from the cursor 
    position it can determine what method / function the cursor position 
    is a part of.  Once it knows the function name it can retrieve the 
    parameters that are being sent to the method as well as determine 
    whether the method has any return types.  Based on that information 
    it will return a docstring template the uses the Sphinx documentation 
    systems tags, in a restructured text format that Sphinx can consume.
    
    This is a revised version of the original method GetMethodArg.  This new 
    version will use the pydev ast document structure.  In other words uses
    the same code that pydev uses to parse the individual elements in the 
    function.  
    
    Versions prior to this version retrieved the functions code, and parsed
    it using my own simple python based function parser.
    
    :param  idocument: The input document context.  From this object you can get an iDocument
                       object, and from there get any parameter you require.
    :type idocument: org.python.pydev.editor.codecompletion.templates.PyDocumentTemplateContext
    
    :returns: a docstring for the method that the cursor was in when this
              method was invoked through the templateing system in pydev.
    :rtype: string
    '''
    from org.python.pydev.parser.fastparser import FastParser
    from org.python.pydev.core.docutils import PySelection
    from org.python.pydev.parser.jython.ast import FunctionDef
    
    docString = None
    print 'using method:GetMethodArg2'
    print 'here'
    # 1 - get the current document
    doc = idocument.getDocument()
    # 2 - create a selection object that gets the line that this method gets called 
    #     from, ie the line the cursor was on when this method is called, or again
    #     the position where the returned docstring will get inserted.
    #selection = PySelection(doc, idocument.getStart())
    selection = idocument.createPySelection()
    lineStart = selection.getPreviousLineThatStartsScope()
    startLineNumber = doc.getLineOfOffset(idocument.getStart())
    print 'startlinenumber', startLineNumber, lineStart.iLineStartingScope
    
    # 3 find out the start line of the function that encloses  
    #   the cursor position from which this function was called
    # the fast parser is a quick way to get the class and function start lines.
    #parser = FastParser.parseToKnowGloballyAccessiblePath(idocument.getDocument(), selection.getStartLineIndex())
    parser = FastParser.parseToKnowGloballyAccessiblePath(doc, startLineNumber)
    print 'got here now! now'
    if parser:
        from java.util import Collections
        Collections.reverse(parser)    
        print 'parser-----'
        print parser
        for stmt in parser:
            print 'stmt', stmt
            # found a function definition, can now retrieve the line
            # number of the function definition
            if isinstance(stmt, FunctionDef):
                # Getting the line number of the func def here.
                #print 'method line', stmt.beginLine
                # Now going to do a full parse of the doc and retrieve 
                # the full function definition info (fast parser just 
                # gets the headlines, this parser will retrieve the 
                # full function def in an AST hierarchy from which 
                # we can extract args and return values.
                functionDefinitionStmt = selection.getLine(stmt.beginLine-1)
                print 'defstatement:', functionDefinitionStmt
                docString = getFuncArgs(idocument, stmt.beginLine)
                # found what we need, no need to continue wiht the rest of the doc
                break
    return docString