Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid extra newlines #203

Merged
merged 9 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: NilsJPWerner
21 changes: 21 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Publish autoDocstring
on:
push:
tags:
- "*"

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 14
cache: "npm"
- run: npm ci
- name: Publish to Visual Studio Marketplace
uses: HaaLeo/publish-vscode-extension@v1
with:
pat: ${{ secrets.VS_MARKETPLACE_TOKEN }}
registryUrl: https://marketplace.visualstudio.com
38 changes: 38 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Test autoDocstring
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
unit_tests:
runs-on: "${{ matrix.os }}"
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node: [14]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "${{ matrix.node-version }}"
cache: "npm"
- run: npm install
- run: npm run vscode:prepublish
- run: npm run unit_test

integration_tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 14
cache: "npm"
- run: npm install
- uses: GabrielBB/xvfb-action@v1
with:
options: -screen 0 1024x768x24
run: npm run integration_test
36 changes: 0 additions & 36 deletions .travis.yml

This file was deleted.

6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![Build Status](https://travis-ci.org/NilsJPWerner/autoDocstring.svg?branch=master)](https://travis-ci.org/NilsJPWerner/autoDocstring)
[![Installs](https://vsmarketplacebadge.apphb.com/installs-short/njpwerner.autodocstring.svg)](https://marketplace.visualstudio.com/items?itemName=njpwerner.autodocstring)
[![Rating](https://vsmarketplacebadge.apphb.com/rating-short/njpwerner.autodocstring.svg)](https://marketplace.visualstudio.com/items?itemName=njpwerner.autodocstring&ssr=false#review-details)
[![Build Status](https://github.com/NilsJPWerner/autoDocstring/actions/workflows/test.yml/badge.svg)](https://github.com/NilsJPWerner/autoDocstring/actions/workflows/test.yml)
[![Github Sponsorship](https://img.shields.io/badge/sponsor-5A5A5A?style=flat&logo=GitHub-Sponsors)](https://github.com/sponsors/NilsJPWerner)

# VSCode Python Docstring Generator

Expand All @@ -21,13 +22,14 @@ Visual Studio Code extension to quickly generate docstrings for python functions
- docBlockr
- Numpy
- Sphinx
- oneline-rst
- PEP0257 (coming soon)

## Usage

Cursor must be on the line directly below the definition to generate full auto-populated docstring

- Press enter after opening docstring with triple quotes (`"""` or `'''`)
- Press enter after opening docstring with triple quotes (configurable `"""` or `'''`)
- Keyboard shortcut: `ctrl+shift+2` or `cmd+shift+2` for mac
- Can be changed in Preferences -> Keyboard Shortcuts -> extension.generateDocstring
- Command: `Generate Docstring`
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@
"enum": [
"docblockr",
"google",
"google-notypes",
"sphinx",
"numpy"
"numpy",
"oneline-rst"
],
"description": "Which docstring format to use."
},
Expand Down
2 changes: 2 additions & 0 deletions src/docstring/get_template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export function getTemplate(docstringFormat: string): string {
switch (docstringFormat) {
case "google":
return getTemplateFile("google.mustache");
case "google-notypes":
return getTemplateFile("google-notypes.mustache");
case "sphinx":
return getTemplateFile("sphinx.mustache");
case "numpy":
Expand Down
10 changes: 5 additions & 5 deletions src/docstring/templates/default.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,36 @@
<%summaryPlaceholder%>

<%extendedSummaryPlaceholder%>

<%#argsExist%>

Arguments:
<%#args%>
<%var%> {<%typePlaceholder%>} -- <%descriptionPlaceholder%>
<%/args%>
<%/argsExist%>

<%#kwargsExist%>

Keyword Arguments:
<%#kwargs%>
<%var%> {<%typePlaceholder%>} -- <%descriptionPlaceholder%> (default: {<%&default%>})
<%/kwargs%>
<%/kwargsExist%>

<%#exceptionsExist%>

Raises:
<%#exceptions%>
<%type%>: <%descriptionPlaceholder%>
<%/exceptions%>
<%/exceptionsExist%>

<%#returnsExist%>

Returns:
<%#returns%>
<%typePlaceholder%> -- <%descriptionPlaceholder%>
<%/returns%>
<%/returnsExist%>

<%#yieldsExist%>

Yields:
<%#yields%>
<%typePlaceholder%> -- <%descriptionPlaceholder%>
Expand Down
35 changes: 35 additions & 0 deletions src/docstring/templates/google-notypes.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{{! Google Docstring Template without Types for Args, Returns or Yields }}
{{summaryPlaceholder}}

{{extendedSummaryPlaceholder}}

{{#parametersExist}}
Args:
{{#args}}
{{var}}: {{descriptionPlaceholder}}
{{/args}}
{{#kwargs}}
{{var}}: {{descriptionPlaceholder}}. Defaults to {{&default}}.
{{/kwargs}}
{{/parametersExist}}

{{#exceptionsExist}}
Raises:
{{#exceptions}}
{{type}}: {{descriptionPlaceholder}}
{{/exceptions}}
{{/exceptionsExist}}

{{#returnsExist}}
Returns:
{{#returns}}
{{descriptionPlaceholder}}
{{/returns}}
{{/returnsExist}}

{{#yieldsExist}}
Yields:
{{#yields}}
{{descriptionPlaceholder}}
{{/yields}}
{{/yieldsExist}}
8 changes: 4 additions & 4 deletions src/docstring/templates/google.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{{summaryPlaceholder}}

{{extendedSummaryPlaceholder}}

{{#parametersExist}}

Args:
{{#args}}
{{var}} ({{typePlaceholder}}): {{descriptionPlaceholder}}
Expand All @@ -12,22 +12,22 @@ Args:
{{var}} ({{typePlaceholder}}, optional): {{descriptionPlaceholder}}. Defaults to {{&default}}.
{{/kwargs}}
{{/parametersExist}}

{{#exceptionsExist}}

Raises:
{{#exceptions}}
{{type}}: {{descriptionPlaceholder}}
{{/exceptions}}
{{/exceptionsExist}}

{{#returnsExist}}

Returns:
{{#returns}}
{{typePlaceholder}}: {{descriptionPlaceholder}}
{{/returns}}
{{/returnsExist}}

{{#yieldsExist}}

Yields:
{{#yields}}
{{typePlaceholder}}: {{descriptionPlaceholder}}
Expand Down
10 changes: 5 additions & 5 deletions src/docstring/templates/numpy.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{{summaryPlaceholder}}

{{extendedSummaryPlaceholder}}

{{#parametersExist}}

Parameters
----------
{{#args}}
Expand All @@ -15,26 +15,26 @@ Parameters
{{descriptionPlaceholder}}, by default {{&default}}
{{/kwargs}}
{{/parametersExist}}

{{#returnsExist}}

Returns
-------
{{#returns}}
{{typePlaceholder}}
{{descriptionPlaceholder}}
{{/returns}}
{{/returnsExist}}

{{#yieldsExist}}

Yields
-------
------
{{#yields}}
{{typePlaceholder}}
{{descriptionPlaceholder}}
{{/yields}}
{{/yieldsExist}}

{{#exceptionsExist}}

Raises
------
{{#exceptions}}
Expand Down
2 changes: 1 addition & 1 deletion src/parse/guess_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function guessType(parameter: string): string {
}

function getTypeFromTyping(parameter: string): string {
const pattern = /\w+\s*:\s*(['"]?\w[\w\[\], \.]*['"]?)/;
const pattern = /\w+\s*:\s*(['"]?\w[\w\[\], |\.]*['"]?)/;
const typeHint = pattern.exec(parameter);

if (typeHint == null || typeHint.length !== 2) {
Expand Down
2 changes: 1 addition & 1 deletion src/parse/parse_parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function parseYields(parameters: string[], body: string[]): Yields {
}

function parseReturnFromDefinition(parameters: string[]): Returns | null {
const pattern = /^->\s*(["']?)([\w\[\], \.]*)\1/;
const pattern = /^->\s*(["']?)([\w\[\], |\.]*)\1/;

for (const param of parameters) {
const match = param.trim().match(pattern);
Expand Down
10 changes: 8 additions & 2 deletions src/parse/tokenize_definition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function tokenizeDefinition(functionDefinition: string): string[] {
const definitionPattern = /(?:def|class)\s+\w+\s*\(([\s\S]*)\)\s*(->\s*(["']?)[\w\[\], \.]*\3)?:\s*(?:#.*)?$/;
const definitionPattern = /(?:def|class)\s+\w+\s*\(([\s\S]*)\)\s*(->\s*(["']?)[\w\[\], |\.]*\3)?:\s*(?:#.*)?$/;

const match = definitionPattern.exec(functionDefinition);
if (match == undefined || match[1] == undefined) {
Expand All @@ -24,7 +24,7 @@ function tokenizeParameterString(parameterString: string): string[] {

while (position >= 0) {
const top = stack[stack.length - 1];
const char = parameterString.charAt(position);
let char = parameterString.charAt(position);

/* todo
'<' char,
Expand Down Expand Up @@ -81,6 +81,12 @@ function tokenizeParameterString(parameterString: string): string[] {
case char === "\t" && stack.length === 0:
position -= 1;
continue;

// 9. Surround pipe character with whitespace
case char === "|":
char = ` ${char}`;
arg = ` ${arg}`;
break;
}

arg = char + arg;
Expand Down
8 changes: 8 additions & 0 deletions src/test/docstring/get_template.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ describe("getTemplate()", () => {
});
});

context("when asked for google-notypes template", () => {
it("should return the string containing the google mustache template", () => {
const result = getTemplate("google-notypes");

expect(result).to.contain("Google Docstring Template without Types for Args, Returns or Yields");
});
});

context("when asked for sphinx template", () => {
it("should return the string containing the sphinx mustache template", () => {
const result = getTemplate("sphinx");
Expand Down
11 changes: 11 additions & 0 deletions src/test/integration/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ describe("Basic Integration Tests", function () {
});
});

it("generates a docstring using PEP 604 style type hints in file 7", async function () {
await testDocstringGeneration({
expectedOutputFilePath: path.resolve(
__dirname,
"./python_test_files/file_7_output.py",
),
inputFilePath: path.resolve(__dirname, "./python_test_files/file_7.py"),
position: new vsc.Position(8, 0),
});
});

it("generates a docstring for the starlark function", async function () {
await testDocstringGeneration({
expectedOutputFilePath: path.resolve(
Expand Down
10 changes: 10 additions & 0 deletions src/test/integration/python_test_files/file_7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from __future__ import annotations


def function(
arg1: int,
arg2: list[str] | dict[str, int] | Thing,
kwarg1: int | float = 1
) -> list[str] | dict[str, int] | Thing:

return arg2
Loading