Search in sources :

Example 1 with DocstringInfo

use of org.python.pydev.core.docutils.PySelection.DocstringInfo in project Pydev by fabioz.

the class AssistDocString method getProps.

/**
 * @see org.python.pydev.editor.correctionassist.IAssistProps#getProps(org.python.pydev.core.docutils.PySelection,
 *      org.python.pydev.shared_ui.ImageCache)
 */
@Override
public List<ICompletionProposalHandle> getProps(PySelection ps, IImageCache imageCache, File f, IPythonNature nature, IPyEdit edit, int offset) throws BadLocationException {
    ArrayList<ICompletionProposalHandle> l = new ArrayList<>();
    Tuple<List<String>, Integer> tuple = ps.getInsideParentesisToks(false);
    if (tuple == null) {
        if (ps.isInClassLine()) {
            tuple = new Tuple<List<String>, Integer>(new ArrayList<String>(), offset);
        } else {
            return l;
        }
    }
    List<String> params = tuple.o1;
    int lineOfOffset = ps.getLineOfOffset(tuple.o2);
    // Calculate only the initial part of the docstring here (everything else should be lazily computed on apply).
    String initial = PySelection.getIndentationFromLine(ps.getCursorLineContents());
    String delimiter = PyAction.getDelimiter(ps.getDoc());
    String indentation = edit != null ? edit.getIndentPrefs().getIndentationString() : DefaultIndentPrefs.get(nature).getIndentationString();
    String delimiterAndIndent = delimiter + initial + indentation;
    FastStringBuffer buf = new FastStringBuffer();
    String docStringMarker = DocstringsPrefPage.getDocstringMarker();
    buf.append(delimiterAndIndent + docStringMarker);
    buf.append(delimiterAndIndent);
    int newOffset = buf.length();
    int offsetPosToAdd = ps.getEndLineOffset(lineOfOffset);
    // may be null (testing)
    IImageHandle image = null;
    if (imageCache != null) {
        image = imageCache.get(UIConstants.ASSIST_DOCSTRING);
    }
    final boolean inFunctionLine = ps.isInFunctionLine(true);
    DocstringInfo docstringFromFunction = null;
    if (inFunctionLine) {
        int currLine = ps.getLineOfOffset();
        docstringFromFunction = ps.getDocstringFromLine(currLine + 1);
    }
    final DocstringInfo finalDocstringFromFunction = docstringFromFunction;
    String preferredDocstringStyle = AssistDocString.this.docStringStyle;
    if (preferredDocstringStyle == null) {
        preferredDocstringStyle = DocstringsPrefPage.getPreferredDocstringStyle();
    }
    final String preferredDocstringStyle2 = preferredDocstringStyle;
    if (preferredDocstringStyle.equals(DocstringsPrefPage.DOCSTRINGSTYLE_GOOGLE)) {
        buf.append("Args:");
    }
    l.add(CompletionProposalFactory.get().createAssistDocstringCompletionProposal("", offsetPosToAdd, 0, newOffset, image, finalDocstringFromFunction != null ? "Update docstring" : "Make docstring", null, null, IPyCompletionProposal.PRIORITY_DEFAULT, null, initial, delimiter, docStringMarker, delimiterAndIndent, preferredDocstringStyle2, inFunctionLine, finalDocstringFromFunction, indentation, buf, params));
    return l;
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) ArrayList(java.util.ArrayList) DocstringInfo(org.python.pydev.core.docutils.PySelection.DocstringInfo) IImageHandle(org.python.pydev.shared_core.image.IImageHandle) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with DocstringInfo

use of org.python.pydev.core.docutils.PySelection.DocstringInfo in project Pydev by fabioz.

the class PySelectionTest method testGetDocstringFromLine.

public void testGetDocstringFromLine() throws BadLocationException {
    doc = new Document("def m1():\n    'docstring'");
    ps = new PySelection(doc);
    DocstringInfo docstringFromLine = ps.getDocstringFromLine(1);
    assertEquals("DocstringInfo [startLiteralOffset=14, endLiteralOffset=25, string='docstring']", docstringFromLine.toString());
    assertEquals(11, docstringFromLine.getLength());
    assertEquals("'docstring'", doc.get(docstringFromLine.startLiteralOffset, docstringFromLine.getLength()));
}
Also used : DocstringInfo(org.python.pydev.core.docutils.PySelection.DocstringInfo) PySelection(org.python.pydev.core.docutils.PySelection) Document(org.eclipse.jface.text.Document)

Example 3 with DocstringInfo

use of org.python.pydev.core.docutils.PySelection.DocstringInfo in project Pydev by fabioz.

the class PySelectionTest method testGetDocstringFromLineInvalid.

public void testGetDocstringFromLineInvalid() {
    doc = new Document("def m1():\n    '''docstring\n    ''");
    ps = new PySelection(doc);
    DocstringInfo docstringFromLine = ps.getDocstringFromLine(1);
    assertNull(docstringFromLine);
}
Also used : DocstringInfo(org.python.pydev.core.docutils.PySelection.DocstringInfo) PySelection(org.python.pydev.core.docutils.PySelection) Document(org.eclipse.jface.text.Document)

Example 4 with DocstringInfo

use of org.python.pydev.core.docutils.PySelection.DocstringInfo in project Pydev by fabioz.

the class PySelectionTest method testGetDocstringFromLine2.

public void testGetDocstringFromLine2() throws BadLocationException {
    doc = new Document("def m1():\n    '''docstring\n    '''");
    ps = new PySelection(doc);
    DocstringInfo docstringFromLine = ps.getDocstringFromLine(1);
    assertEquals("DocstringInfo [startLiteralOffset=14, endLiteralOffset=34, string='''docstring\n" + "    ''']", docstringFromLine.toString());
    assertEquals("'''docstring\n    '''", doc.get(docstringFromLine.startLiteralOffset, docstringFromLine.getLength()));
}
Also used : DocstringInfo(org.python.pydev.core.docutils.PySelection.DocstringInfo) PySelection(org.python.pydev.core.docutils.PySelection) Document(org.eclipse.jface.text.Document)

Aggregations

DocstringInfo (org.python.pydev.core.docutils.PySelection.DocstringInfo)4 Document (org.eclipse.jface.text.Document)3 PySelection (org.python.pydev.core.docutils.PySelection)3 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ICompletionProposalHandle (org.python.pydev.shared_core.code_completion.ICompletionProposalHandle)1 IImageHandle (org.python.pydev.shared_core.image.IImageHandle)1 FastStringBuffer (org.python.pydev.shared_core.string.FastStringBuffer)1