Skip to content

Commit fbebc99

Browse files
committed
Add encoding preference
1 parent 590d1bf commit fbebc99

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

src/org/phpsrc/eclipse/pti/tools/codesniffer/core/PHPCodeSniffer.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ protected PHPToolLauncher getPHPToolLauncher(IProject project,
225225
.getSessionProperty(QUALIFIED_NAME);
226226
if (launcher != null) {
227227
launcher.setCommandLineArgs(getCommandLineArgs(standard,
228-
prefs.getTabWidth()));
228+
prefs.getTabWidth(), prefs.getEncoding()));
229229
return launcher;
230230
}
231231
} catch (CoreException e) {
@@ -234,7 +234,7 @@ protected PHPToolLauncher getPHPToolLauncher(IProject project,
234234

235235
launcher = new PHPToolLauncher(QUALIFIED_NAME,
236236
getPHPExecutable(prefs.getPhpExecutable()), getScriptFile(),
237-
getCommandLineArgs(standard, prefs.getTabWidth()),
237+
getCommandLineArgs(standard, prefs.getTabWidth(), prefs.getEncoding()),
238238
getPHPINIEntries(prefs, project, standard));
239239

240240
launcher.setPrintOuput(prefs.isPrintOutput());
@@ -279,7 +279,7 @@ public static IPath getScriptFile() {
279279
"/php/tools/phpcs.php");
280280
}
281281

282-
private String getCommandLineArgs(Standard standard, int tabWidth) {
282+
private String getCommandLineArgs(Standard standard, int tabWidth, String encoding) {
283283

284284
String args = "--report=xml --standard="
285285
+ (standard.custom ? OperatingSystem
@@ -289,6 +289,9 @@ private String getCommandLineArgs(Standard standard, int tabWidth) {
289289
if (tabWidth > 0)
290290
args += " --tab-width=" + tabWidth;
291291

292+
if (encoding != null && !encoding.isEmpty())
293+
args += " --encoding=" + encoding;
294+
292295
return args + " " + PHPToolLauncher.COMMANDLINE_PLACEHOLDER_FILE;
293296
}
294297
}

src/org/phpsrc/eclipse/pti/tools/codesniffer/core/preferences/PHPCodeSnifferPreferences.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ public class PHPCodeSnifferPreferences extends AbstractPEARPHPToolPreferences {
1616
protected String ignorePattern;
1717
protected String[] ignoreSniffs;
1818
protected String[] fileExtensions;
19+
protected String encoding;
1920

2021
public PHPCodeSnifferPreferences(String phpExecutable, boolean printOutput, String pearLibraryName,
21-
Standard[] standards, int tabWidth, String[] fileExtensions, String ignorePattern, String[] ignoreSniffs) {
22+
Standard[] standards, int tabWidth, String[] fileExtensions, String ignorePattern, String[] ignoreSniffs,
23+
String encoding) {
2224
super(phpExecutable, printOutput, pearLibraryName);
2325
this.standards = standards;
2426
this.tabWidth = tabWidth;
@@ -28,6 +30,7 @@ public PHPCodeSnifferPreferences(String phpExecutable, boolean printOutput, Stri
2830
else
2931
this.ignorePattern = null;
3032
this.ignoreSniffs = ignoreSniffs;
33+
this.encoding = encoding;
3134
}
3235

3336
public Standard[] getStandards() {
@@ -49,4 +52,8 @@ public String getIgnorePattern() {
4952
public String[] getIgnoreSniffs() {
5053
return ignoreSniffs;
5154
}
55+
56+
public String getEncoding() {
57+
return encoding;
58+
}
5259
}

src/org/phpsrc/eclipse/pti/tools/codesniffer/core/preferences/PHPCodeSnifferPreferencesFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public static PHPCodeSnifferPreferences factory(IProject project) {
4949
boolean printOutput = prefs.getBoolean(PHPCodeSnifferPreferenceNames.PREF_DEBUG_PRINT_OUTPUT);
5050
String ignorePattern = prefs.getString(PHPCodeSnifferPreferenceNames.PREF_IGNORE_PATTERN);
5151
String ignoreSniffs = prefs.getString(PHPCodeSnifferPreferenceNames.PREF_IGNORE_SNIFFS);
52+
String encoding = prefs.getString(PHPCodeSnifferPreferenceNames.PREF_ENCODING);
5253

5354
IScopeContext[] preferenceScopes = createPreferenceScopes(project);
5455
if (preferenceScopes[0] instanceof ProjectScope) {
@@ -108,7 +109,7 @@ public static PHPCodeSnifferPreferences factory(IProject project) {
108109

109110
return new PHPCodeSnifferPreferences(phpExe, printOutput, pearLibraryName, standards.toArray(new Standard[0]),
110111
tabWidth, fileExtensionsList, ignorePattern, ignoreSniffs == null || ignoreSniffs.length() == 0 ? null
111-
: ignoreSniffs.split(" *, *"));
112+
: ignoreSniffs.split(" *, *"), encoding);
112113
}
113114

114115
protected static IScopeContext[] createPreferenceScopes(IProject project) {

src/org/phpsrc/eclipse/pti/tools/codesniffer/ui/preferences/PHPCodeSnifferConfigurationBlock.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class PHPCodeSnifferConfigurationBlock extends AbstractPEARPHPToolConfigu
6161
private static final Key PREF_FILE_EXTENSIONS = getCodeSnifferKey(PHPCodeSnifferPreferenceNames.PREF_FILE_EXTENSIONS);
6262
private static final Key PREF_IGNORE_PATTERN = getCodeSnifferKey(PHPCodeSnifferPreferenceNames.PREF_IGNORE_PATTERN);
6363
private static final Key PREF_IGNORE_SNIFFS = getCodeSnifferKey(PHPCodeSnifferPreferenceNames.PREF_IGNORE_SNIFFS);
64+
private static final Key PREF_ENCODING = getCodeSnifferKey(PHPCodeSnifferPreferenceNames.PREF_ENCODING);
6465

6566
private static final int IDX_ADD = 0;
6667
private static final int IDX_EDIT = 1;
@@ -71,6 +72,7 @@ public class PHPCodeSnifferConfigurationBlock extends AbstractPEARPHPToolConfigu
7172
private final StringDialogField fFileExtension;
7273
private final StringDialogField fIgnorePattern;
7374
private final StringDialogField fIgnoreSniffs;
75+
private final StringDialogField fEncoding;
7476

7577
private class CodeSnifferLabelProvider extends LabelProvider implements ITableLabelProvider, IFontProvider {
7678

@@ -231,12 +233,17 @@ public PHPCodeSnifferConfigurationBlock(IStatusChangeListener context, IProject
231233
fIgnoreSniffs.setLabelText("Sniffs:");
232234

233235
unpackIgnoreSniffs();
236+
237+
fEncoding = new StringDialogField();
238+
fEncoding.setLabelText("Encoding:");
239+
240+
unpackEncoding();
234241
}
235242

236243
private static Key[] getKeys() {
237244
return new Key[] { PREF_PHP_EXECUTABLE, PREF_PEAR_LIBRARY, PREF_DEBUG_PRINT_OUTPUT, PREF_CUSTOM_STANDARD_NAMES,
238245
PREF_CUSTOM_STANDARD_PATHS, PREF_ACTIVE_STANDARDS, PREF_DEFAULT_TAB_WITH, PREF_FILE_EXTENSIONS,
239-
PREF_IGNORE_PATTERN, PREF_IGNORE_SNIFFS };
246+
PREF_IGNORE_PATTERN, PREF_IGNORE_SNIFFS, PREF_ENCODING };
240247
}
241248

242249
protected Composite createToolContents(Composite parent) {
@@ -298,6 +305,9 @@ private Composite createStandardsTabContent(Composite folder) {
298305
createDialogFieldsWithInfoText(folder, new DialogField[] { fIgnoreSniffs }, "Ignore Sniffs",
299306
new String[] { "Sniffs are separated by a comma" });
300307

308+
createDialogFieldsWithInfoText(folder, new DialogField[] { fEncoding }, "Source Encoding",
309+
new String[] { "PHP source file encoding" });
310+
301311
unpackStandards(pearLibraryCombo.getText());
302312
pearLibraryCombo.addSelectionListener(new SelectionListener() {
303313
public void widgetSelected(SelectionEvent e) {
@@ -385,6 +395,7 @@ protected boolean processChanges(IWorkbenchPreferenceContainer container) {
385395
setValue(PREF_FILE_EXTENSIONS, "" + fFileExtension.getText());
386396
setValue(PREF_IGNORE_PATTERN, fIgnorePattern.getText());
387397
setValue(PREF_IGNORE_SNIFFS, fIgnoreSniffs.getText());
398+
setValue(PREF_ENCODING, fEncoding.getText());
388399

389400
return super.processChanges(container);
390401
}
@@ -460,6 +471,14 @@ private void unpackIgnoreSniffs() {
460471
fIgnoreSniffs.setText(ignoreSniffs);
461472
}
462473

474+
private void unpackEncoding() {
475+
String encoding = getValue(PREF_ENCODING);
476+
if (encoding != null)
477+
fEncoding.setText(encoding);
478+
else
479+
fEncoding.setText("UTF-8");
480+
}
481+
463482
private void unpackStandards(String libName) {
464483
String activeStandards = getValue(PREF_ACTIVE_STANDARDS);
465484
ArrayList<String> activeList = new ArrayList<String>();

src/org/phpsrc/eclipse/pti/tools/codesniffer/ui/preferences/PHPCodeSnifferPreferenceNames.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ public class PHPCodeSnifferPreferenceNames {
1616
public static final String PREF_CUSTOM_STANDARD_PATHS = "custom_standard_paths"; //$NON-NLS-1$
1717
public static final String PREF_DEFAULT_STANDARD_NAME = "default_standard_name"; //$NON-NLS-1$
1818
public static final String PREF_DEFAULT_STANDARD_PATH = "default_standard_path"; //$NON-NLS-1$
19-
public static final String PREF_ACTIVE_STANDARDS = "active_standards"; //$NON-NLS-1$
19+
public static final String PREF_ACTIVE_STANDARDS = "active_standards"; //$NON-NLS-1$
2020
public static final String PREF_DEFAULT_TAB_WITH = "default_tab_width"; //$NON-NLS-1$
2121
public static final String PREF_FILE_EXTENSIONS = "file_extension"; //$NON-NLS-1$
2222
public static final String PREF_IGNORE_PATTERN = "ignore_pattern"; //$NON-NLS-1$
2323
public static final String PREF_IGNORE_SNIFFS = "pref_ignore_sniffs"; //$NON-NLS-1$
24+
public static final String PREF_ENCODING = "pref_sniffs"; //$NON-NLS-1$
2425
}

0 commit comments

Comments
 (0)