Skip to content

Commit

Permalink
Move docstring preferences to core.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Aug 26, 2024
1 parent 1006a6b commit 180c308
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 137 deletions.
1 change: 1 addition & 0 deletions plugins/org.python.pydev.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Export-Package: org.python.copiedfromeclipsesrc,
org.python.pydev.core.cache,
org.python.pydev.core.concurrency,
org.python.pydev.core.cython,
org.python.pydev.core.docstrings,
org.python.pydev.core.docutils,
org.python.pydev.core.editor,
org.python.pydev.core.formatter,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.python.pydev.core.docstrings;

import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.python.pydev.core.preferences.PydevPrefs;
import org.python.pydev.shared_core.SharedCorePlugin;

public class DocstringPreferences {

/* Preference identifiers */
public static final String DOCSTRING_CHARACTER = "DOCSTRING CHARACTER";
public static final String DEFAULT_DOCSTRING_CHARACTER = "'";

public static final String DOCSTRING_STYLE = "DOCSTRING STYLE";
public static final String DOCSTRING_STYLE_SPHINX = ":";
public static final String DOCSTRING_STYLE_EPYDOC = "@";
public static final String DOCSTRING_STYLE_GOOGLE = "G";
public static final String DEFAULT_DOCSTRING_STYLE = DOCSTRING_STYLE_SPHINX;

public static final String TYPETAG_GENERATION_NEVER = "Never";
public static final String TYPETAG_GENERATION_ALWAYS = "Always";
public static final String TYPETAG_GENERATION_CUSTOM = "Custom";
public static final String TYPETAG_GENERATION = "TYPETAGGENERATION";
public static final String DEFAULT_TYPETAG_GENERATION = TYPETAG_GENERATION_NEVER;

public static final String DONT_GENERATE_TYPETAGS = "DONT_GENERATE_TYPETAGS_PREFIXES";
public static final String DEFAULT_DONT_GENERATE_TYPETAGS = "sz\0n\0f";

/**
* Getter for the preferred docstring character. Only a shortcut.
*
* @return
*/
public static String getPreferredDocstringCharacter() {
if (SharedCorePlugin.inTestMode()) {
return "'";//testing...

}
return PydevPrefs.getEclipsePreferences().get(DOCSTRING_CHARACTER, DEFAULT_DOCSTRING_CHARACTER);
}

public static String getPreferredDocstringStyle() {
if (SharedCorePlugin.inTestMode()) {
return ":"; //testing
}

return PydevPrefs.getEclipsePreferences().get(DOCSTRING_STYLE, DEFAULT_DOCSTRING_STYLE);
}

public final static Map<String, String> strToMarker = new HashMap<String, String>();

static {
DocstringPreferences.strToMarker.put("'", "'''");
DocstringPreferences.strToMarker.put("\"", "\"\"\"");
}

/**
*
* @return The string that should be used to mark the beginning or end of a
* docstring. (""") or (''')
*/
public static String getDocstringMarker() {
String docstringChar = getPreferredDocstringCharacter();
String ret = strToMarker.get(docstringChar);
if (ret == null) {
ret = docstringChar + docstringChar + docstringChar;
strToMarker.put(docstringChar, ret);
}
return ret;
}

public static boolean GENERATE_TYPE_DOCSTRING_ON_TESTS = true;

/**
* Determines, from the preferences, whether a type tag should be generated
* for a function / method parameter.
*
* @param parameterName The name of the parameter.
* @return true if it should be generated and false otherwise
*/
public static boolean getTypeTagShouldBeGenerated(String parameterName) {
if (SharedCorePlugin.inTestMode()) {
return GENERATE_TYPE_DOCSTRING_ON_TESTS;
}
IEclipsePreferences preferences = PydevPrefs.getEclipsePreferences();
String preference = preferences.get(TYPETAG_GENERATION,
DEFAULT_TYPETAG_GENERATION);
if (preference.equals(TYPETAG_GENERATION_NEVER)) {
return false;
} else if (preference.equals(TYPETAG_GENERATION_ALWAYS)) {
return true;
} else {// TYPETAG_GENERATION_CUSTOM - check prefix.
String prefixesString = preferences.get(DONT_GENERATE_TYPETAGS,
DEFAULT_DONT_GENERATE_TYPETAGS);
StringTokenizer st = new StringTokenizer(prefixesString, "\0"); // "\0" is the separator

while (st.hasMoreTokens()) {
if (parameterName.startsWith(st.nextToken())) {
return false;
}
}

return true; // No match
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.python.pydev.core.IPyEdit;
import org.python.pydev.core.IPythonNature;
import org.python.pydev.core.autoedit.DefaultIndentPrefs;
import org.python.pydev.core.docstrings.DocstringPreferences;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.core.docutils.PySelection.DocstringInfo;
import org.python.pydev.core.proposals.CompletionProposalFactory;
Expand Down Expand Up @@ -86,7 +87,7 @@ public List<ICompletionProposalHandle> getProps(PySelection ps, IImageCache imag
String delimiterAndIndent = delimiter + initial + indentation;

FastStringBuffer buf = new FastStringBuffer();
String docStringMarker = DocstringsPrefPage.getDocstringMarker();
String docStringMarker = DocstringPreferences.getDocstringMarker();
buf.append(delimiterAndIndent + docStringMarker);
buf.append(delimiterAndIndent);

Expand All @@ -106,12 +107,12 @@ public List<ICompletionProposalHandle> getProps(PySelection ps, IImageCache imag
final DocstringInfo finalDocstringFromFunction = docstringFromFunction;
String preferredDocstringStyle = AssistDocString.this.docStringStyle;
if (preferredDocstringStyle == null) {
preferredDocstringStyle = DocstringsPrefPage.getPreferredDocstringStyle();
preferredDocstringStyle = DocstringPreferences.getPreferredDocstringStyle();
}

final String preferredDocstringStyle2 = preferredDocstringStyle;
if (inFunctionLine && params.size() > 0
&& preferredDocstringStyle.equals(DocstringsPrefPage.DOCSTRINGSTYLE_GOOGLE)) {
&& preferredDocstringStyle.equals(DocstringPreferences.DOCSTRING_STYLE_GOOGLE)) {
buf.append("Args:");
}
l.add(CompletionProposalFactory.get().createAssistDocstringCompletionProposal("", offsetPosToAdd, 0,
Expand Down Expand Up @@ -254,7 +255,7 @@ public static String updatedDocstring(String baseDocstring, List<String> params,

ParamInfo existingInfo = paramInfos.get(paramName);
boolean hasParam = existingInfo != null && existingInfo.paramLine != -1;
boolean addTypeForParam = DocstringsPrefPage.getTypeTagShouldBeGenerated(paramName);
boolean addTypeForParam = DocstringPreferences.getTypeTagShouldBeGenerated(paramName);
if (addTypeForParam) {
if (paramsWithTypeInline.contains(paramName)) {
addTypeForParam = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.python.pydev.core.docstrings.DocstringPreferences;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.core.docutils.PySelection.DocstringInfo;
import org.python.pydev.editor.codecompletion.proposals.PyCompletionProposal;
Expand Down Expand Up @@ -77,7 +78,7 @@ public void apply(IDocument document) {
.append(paramName)
.append(":");
}
if (DocstringsPrefPage.getTypeTagShouldBeGenerated(paramName)) {
if (DocstringPreferences.getTypeTagShouldBeGenerated(paramName)) {
buf.append(delimiterAndIndent).append(preferredDocstringStyle2).append("type ")
.append(paramName)
.append(":");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
*/
package org.python.pydev.editor.correctionassist.docstrings;

import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.jface.util.IPropertyChangeListener;
Expand All @@ -19,9 +14,8 @@
import org.eclipse.swt.widgets.Group;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.python.pydev.core.preferences.PydevPrefs;
import org.python.pydev.core.docstrings.DocstringPreferences;
import org.python.pydev.plugin.PydevPlugin;
import org.python.pydev.shared_core.SharedCorePlugin;

/**
* Preferences related to docstrings. These preferences are used by the
Expand All @@ -31,116 +25,12 @@
public class DocstringsPrefPage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage,
IPropertyChangeListener {

/* Preference identifiers */
public static final String P_DOCSTRINGCHARACTER = "DOCSTRING CHARACTER";

public static final String DEFAULT_P_DOCSTRINGCHARACTER = "'";

public static final String P_DOCSTRINGSTYLE = "DOCSTRING STYLE";

public static final String DOCSTRINGSTYLE_SPHINX = ":";

public static final String DOCSTRINGSTYLE_EPYDOC = "@";

public static final String DOCSTRINGSTYLE_GOOGLE = "G";

public static final String DEFAULT_P_DOCSTRINGSTYLE = DOCSTRINGSTYLE_SPHINX;

public static final String TYPETAG_GENERATION_NEVER = "Never";

public static final String TYPETAG_GENERATION_ALWAYS = "Always";

public static final String TYPETAG_GENERATION_CUSTOM = "Custom";

public static final String P_TYPETAGGENERATION = "TYPETAGGENERATION";

public static final String DEFAULT_P_TYPETAGGENERATION = TYPETAG_GENERATION_NEVER;

public static final String P_DONT_GENERATE_TYPETAGS = "DONT_GENERATE_TYPETAGS_PREFIXES";

public static final String DEFAULT_P_DONT_GENERATE_TYPETAGS = "sz\0n\0f";

public DocstringsPrefPage() {
super(GRID);
setPreferenceStore(PydevPlugin.getDefault().getPreferenceStore());
setDescription("Docstring preferences");
}

/**
* Getter for the preferred docstring character. Only a shortcut.
*
* @return
*/
public static String getPreferredDocstringCharacter() {
if (SharedCorePlugin.inTestMode()) {
return "'";//testing...

}
return PydevPrefs.getEclipsePreferences().get(P_DOCSTRINGCHARACTER, DEFAULT_P_DOCSTRINGCHARACTER);
}

public static String getPreferredDocstringStyle() {
if (SharedCorePlugin.inTestMode()) {
return ":"; //testing
}

return PydevPrefs.getEclipsePreferences().get(P_DOCSTRINGSTYLE, DEFAULT_P_DOCSTRINGSTYLE);
}

private final static Map<String, String> strToMarker = new HashMap<String, String>();
static {
strToMarker.put("'", "'''");
strToMarker.put("\"", "\"\"\"");
}

/**
*
* @return The string that should be used to mark the beginning or end of a
* docstring. (""") or (''')
*/
public static String getDocstringMarker() {
String docstringChar = getPreferredDocstringCharacter();
String ret = strToMarker.get(docstringChar);
if (ret == null) {
ret = docstringChar + docstringChar + docstringChar;
strToMarker.put(docstringChar, ret);
}
return ret;
}

public static boolean GENERATE_TYPE_DOCSTRING_ON_TESTS = true;

/**
* Determines, from the preferences, whether a type tag should be generated
* for a function / method parameter.
*
* @param parameterName The name of the parameter.
* @return true if it should be generated and false otherwise
*/
public static boolean getTypeTagShouldBeGenerated(String parameterName) {
if (SharedCorePlugin.inTestMode()) {
return GENERATE_TYPE_DOCSTRING_ON_TESTS;
}
IEclipsePreferences preferences = PydevPrefs.getEclipsePreferences();
String preference = preferences.get(P_TYPETAGGENERATION, DEFAULT_P_TYPETAGGENERATION);
if (preference.equals(TYPETAG_GENERATION_NEVER)) {
return false;
} else if (preference.equals(TYPETAG_GENERATION_ALWAYS)) {
return true;
} else {// TYPETAG_GENERATION_CUSTOM - check prefix.
String prefixesString = preferences.get(P_DONT_GENERATE_TYPETAGS, DEFAULT_P_DONT_GENERATE_TYPETAGS);
StringTokenizer st = new StringTokenizer(prefixesString, "\0"); // "\0" is the separator

while (st.hasMoreTokens()) {
if (parameterName.startsWith(st.nextToken())) {
return false;
}
}

return true; // No match
}
}

/**
* Creates the field editors. Field editors are abstractions of the common
* GUI blocks needed to manipulate various types of preferences. Each field
Expand All @@ -153,27 +43,30 @@ public void createFieldEditors() {
Composite p2 = new Composite(p, 0);
p2.setLayout(new RowLayout());

RadioGroupFieldEditor docstringCharEditor = new RadioGroupFieldEditor(P_DOCSTRINGCHARACTER,
RadioGroupFieldEditor docstringCharEditor = new RadioGroupFieldEditor(DocstringPreferences.DOCSTRING_CHARACTER,
"Docstring character", 1,
new String[][] { { "Quotation mark (\")", "\"" }, { "Apostrophe (')", "'" } }, p2, true);
addField(docstringCharEditor);

RadioGroupFieldEditor docstringStyleEditor = new RadioGroupFieldEditor(P_DOCSTRINGSTYLE, "Docstring style", 1,
new String[][] { { "Sphinx (:tag name:)", DOCSTRINGSTYLE_SPHINX },
{ "EpyDoc (@tag name:)", DOCSTRINGSTYLE_EPYDOC },
{ "Google (name:)", DOCSTRINGSTYLE_GOOGLE }
RadioGroupFieldEditor docstringStyleEditor = new RadioGroupFieldEditor(DocstringPreferences.DOCSTRING_STYLE,
"Docstring style", 1,
new String[][] { { "Sphinx (:tag name:)", DocstringPreferences.DOCSTRING_STYLE_SPHINX },
{ "EpyDoc (@tag name:)", DocstringPreferences.DOCSTRING_STYLE_EPYDOC },
{ "Google (name:)", DocstringPreferences.DOCSTRING_STYLE_GOOGLE }
},
p2, true);
addField(docstringStyleEditor);

Group typeDoctagGroup = new Group(p2, 0);
typeDoctagGroup.setText("Type doctag generation (@type x:...)");
typeDoctagEditor = new RadioGroupFieldEditor(P_TYPETAGGENERATION, "", 1, new String[][] {
{ "&Always", TYPETAG_GENERATION_ALWAYS }, { "&Never", TYPETAG_GENERATION_NEVER },
{ "&Custom", TYPETAG_GENERATION_CUSTOM } }, typeDoctagGroup);
typeDoctagEditor = new RadioGroupFieldEditor(DocstringPreferences.TYPETAG_GENERATION, "", 1, new String[][] {
{ "&Always", DocstringPreferences.TYPETAG_GENERATION_ALWAYS },
{ "&Never", DocstringPreferences.TYPETAG_GENERATION_NEVER },
{ "&Custom", DocstringPreferences.TYPETAG_GENERATION_CUSTOM } }, typeDoctagGroup);

addField(typeDoctagEditor);
addField(new ParameterNamePrefixListEditor(P_DONT_GENERATE_TYPETAGS, "Don't create for parameters with prefix",
addField(new ParameterNamePrefixListEditor(DocstringPreferences.DONT_GENERATE_TYPETAGS,
"Don't create for parameters with prefix",
typeDoctagGroup));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
import org.eclipse.jface.resource.StringConverter;
import org.osgi.service.prefs.Preferences;
import org.python.pydev.builder.todo.PyTodoPrefPage;
import org.python.pydev.core.docstrings.DocstringPreferences;
import org.python.pydev.core.imports.ImportPreferences;
import org.python.pydev.editor.codefolding.PyDevCodeFoldingPrefPage;
import org.python.pydev.editor.commentblocks.CommentBlocksPreferences;
import org.python.pydev.editor.correctionassist.docstrings.DocstringsPrefPage;
import org.python.pydev.editor.hover.PyHoverPreferencesPage;
import org.python.pydev.editor.saveactions.PydevSaveActionsPrefPage;
import org.python.pydev.editorinput.PySourceLocatorPrefs;
Expand Down Expand Up @@ -268,10 +268,10 @@ public void initializeDefaultPreferences() {
PyUnitPrefsPage2.DEFAULT_TEST_RUNNER_DEFAULT_PARAMETERS);

// Docstrings
node.put(DocstringsPrefPage.P_DOCSTRINGCHARACTER, DocstringsPrefPage.DEFAULT_P_DOCSTRINGCHARACTER);
node.put(DocstringsPrefPage.P_DOCSTRINGSTYLE, DocstringsPrefPage.DEFAULT_P_DOCSTRINGSTYLE);
node.put(DocstringsPrefPage.P_TYPETAGGENERATION, DocstringsPrefPage.DEFAULT_P_TYPETAGGENERATION);
node.put(DocstringsPrefPage.P_DONT_GENERATE_TYPETAGS, DocstringsPrefPage.DEFAULT_P_DONT_GENERATE_TYPETAGS);
node.put(DocstringPreferences.DOCSTRING_CHARACTER, DocstringPreferences.DEFAULT_DOCSTRING_CHARACTER);
node.put(DocstringPreferences.DOCSTRING_STYLE, DocstringPreferences.DEFAULT_DOCSTRING_STYLE);
node.put(DocstringPreferences.TYPETAG_GENERATION, DocstringPreferences.DEFAULT_TYPETAG_GENERATION);
node.put(DocstringPreferences.DONT_GENERATE_TYPETAGS, DocstringPreferences.DEFAULT_DONT_GENERATE_TYPETAGS);

//save actions
node.putBoolean(PydevSaveActionsPrefPage.SORT_IMPORTS_ON_SAVE,
Expand Down
Loading

0 comments on commit 180c308

Please sign in to comment.