Skip to content

Commit 968b50f

Browse files
committed
expression help: support optional parameters and defaults
1 parent 9e1be6e commit 968b50f

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

resources/function_help/json/closest_point

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "closest_point",
33
"type": "function",
4-
"description": "Returns the point on geometry 1 that is closest to geometry 2.",
4+
"description": "Returns the point on geometry1 that is closest to geometry2.",
55
"arguments": [
6-
{"arg":"geometry 1","description":"geometry to find closest point on"},
7-
{"arg":"geometry 2","description":"geometry to find closest point to"}
6+
{"arg":"geometry1","description":"geometry to find closest point on"},
7+
{"arg":"geometry2","description":"geometry to find closest point to"}
88
],
99
"examples": [
1010
{

resources/function_help/json/shortest_line

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "shortest_line",
33
"type": "function",
4-
"description": "Returns the shortest line joining geometry 1 to geometry 2. The resultant line will start at geometry 1 and end at geometry 2.",
4+
"description": "Returns the shortest line joining geometry1 to geometry2. The resultant line will start at geometry1 and end at geometry2.",
55
"arguments": [
6-
{"arg":"geometry 1","description":"geometry to find shortest line from"},
7-
{"arg":"geometry 2","description":"geometry to find shortest line to"}
6+
{"arg":"geometry1","description":"geometry to find shortest line from"},
7+
{"arg":"geometry2","description":"geometry to find shortest line to"}
88
],
99
"examples": [
1010
{

scripts/process_function_template.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,14 @@ def quote(v):
8787

8888
if 'arguments' in v:
8989
for a in v['arguments']:
90-
cpp.write("\n << HelpArg( \"{0}\", tr( \"{1}\" ), {2}, {3} )".format(
90+
cpp.write("\n << HelpArg( \"{0}\", tr( \"{1}\" ), {2}, {3}, {4}, \"{5}\" )".format(
9191
a['arg'],
9292
a.get('description', ''),
9393
"true" if a.get('descOnly', False) else "false",
9494
"true" if a.get('syntaxOnly', False) else "false",
9595
"true" if a.get('optional', False) else "false",
96-
a.get('default', ''))
96+
a.get('default', '')
97+
)
9798
)
9899

99100
cpp.write(",\n /* variableLenArguments */ {0}".format(

src/core/expression/qgsexpression.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -575,20 +575,33 @@ QString QgsExpression::helpText( QString name )
575575
{
576576
helpContents += QStringLiteral( "<code><span class=\"functionname\">%1</span>" ).arg( name );
577577

578+
bool hasOptionalArgs = false;
579+
578580
if ( f.mType == tr( "function" ) && ( f.mName[0] != '$' || !v.mArguments.isEmpty() || v.mVariableLenArguments ) )
579581
{
580582
helpContents += '(';
581583

582584
QString delim;
583585
for ( const HelpArg &a : qgis::as_const( v.mArguments ) )
584586
{
585-
helpContents += delim;
586-
delim = QStringLiteral( ", " );
587587
if ( !a.mDescOnly )
588588
{
589-
helpContents += QStringLiteral( "<span class=\"argument %1\">%2%3</span>" ).arg( a.mOptional ? QStringLiteral( "optional" ) : QStringLiteral( "" ), a.mArg,
590-
a.mDefaultVal.isEmpty() ? QLatin1String( "" ) : '=' + a.mDefaultVal );
589+
if ( a.mOptional )
590+
{
591+
hasOptionalArgs = true;
592+
helpContents += QStringLiteral( "[" );
593+
}
594+
595+
helpContents += delim;
596+
helpContents += QStringLiteral( "<span class=\"argument\">%2%3</span>" ).arg(
597+
a.mArg,
598+
a.mDefaultVal.isEmpty() ? QLatin1String( "" ) : '=' + a.mDefaultVal
599+
);
600+
601+
if ( a.mOptional )
602+
helpContents += QStringLiteral( "]" );
591603
}
604+
delim = QStringLiteral( "," );
592605
}
593606

594607
if ( v.mVariableLenArguments )
@@ -600,6 +613,11 @@ QString QgsExpression::helpText( QString name )
600613
}
601614

602615
helpContents += QLatin1String( "</code>" );
616+
617+
if ( hasOptionalArgs )
618+
{
619+
helpContents += QLatin1String( "<br/><br/>" ) + tr( "[ ] marks optional components" );
620+
}
603621
}
604622

605623
if ( !v.mArguments.isEmpty() )

0 commit comments

Comments
 (0)