Skip to content

Commit 3752cca

Browse files
committed
Add all root project env files to ImportantFiles
Closes #9229 - additional coloring lexer fix for single quoted string with slash - adjust env file resolver to match files starting in ".env." - exclude files with lexer and errors extension from env mime type
1 parent 26a05a6 commit 3752cca

8 files changed

Lines changed: 62 additions & 11 deletions

File tree

ide/languages.env/src/org/netbeans/modules/languages/env/EnvFileResolver.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.netbeans.modules.languages.env;
2020

21+
import java.util.Set;
2122
import org.netbeans.api.annotations.common.CheckForNull;
2223
import org.netbeans.api.annotations.common.NonNull;
2324
import org.openide.awt.ActionID;
@@ -92,9 +93,16 @@
9293

9394
@ServiceProvider(service = MIMEResolver.class)
9495
public class EnvFileResolver extends MIMEResolver {
96+
9597
public static final String ENV_EXT = "env"; //NOI18N
98+
public static final String DOT_ENV = "." + ENV_EXT; //NOI18N
9699
public static final String MIME_TYPE = "text/x-env"; //NOI18N
97100

101+
/**
102+
* extensions to exclude from mime association
103+
*/
104+
private static final Set<String> EXCLUDED_EXTENSIONS = Set.of("lexer", "errors"); //NOI18N
105+
98106
public EnvFileResolver() {
99107
super(MIME_TYPE);
100108
}
@@ -103,17 +111,17 @@ public EnvFileResolver() {
103111
@Override
104112
public String findMIMEType(@NonNull final FileObject fo) {
105113
final String nameWithExt = fo.getNameExt().toLowerCase();
114+
final String ext = fo.getExt();
106115

107-
if (nameWithExt.endsWith("." + ENV_EXT)) { //NOI18N
108-
return MIME_TYPE;
116+
if (EXCLUDED_EXTENSIONS.contains(ext)) {
117+
return null;
109118
}
110119

111-
//Some application might use .env.example name format
112-
int envPartPosition = 2;
113-
String[] nameParts = nameWithExt.split("\\."); //NOI18N
114-
115-
//check for previous name part
116-
if (nameParts.length >= envPartPosition && nameParts[nameParts.length - envPartPosition].equals(ENV_EXT)) {
120+
if (ext.equals(ENV_EXT)
121+
|| nameWithExt.equals(DOT_ENV)
122+
//env files naming usually used in JS, PHP, Python frameworks
123+
//.env.dist, .env.local, .env.local.demo ...
124+
|| nameWithExt.startsWith(DOT_ENV + ".")) { //NOI18N
117125
return MIME_TYPE;
118126
}
119127

ide/languages.env/src/org/netbeans/modules/languages/env/grammar/antlr4/coloring/EnvAntlrColoringLexer.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fragment BackTickQuote
7676
;
7777

7878
fragment SQuoteLiteral
79-
: SQuote (Esc [btnfr"'\\] | ~ ['\\])* SQuote
79+
: SQuote ( Esc ~[\r\n] | ~[\\'\r\n])* SQuote
8080
;
8181
8282
fragment NewLine

ide/languages.env/src/org/netbeans/modules/languages/env/project/EnvFileImpl.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,36 @@
1818
*/
1919
package org.netbeans.modules.languages.env.project;
2020

21+
import java.util.ArrayList;
2122
import java.util.Collection;
23+
import java.util.List;
2224
import javax.swing.event.ChangeListener;
2325
import org.netbeans.api.project.Project;
26+
import org.netbeans.modules.languages.env.EnvFileResolver;
2427
import org.netbeans.modules.web.common.spi.ImportantFilesImplementation;
2528
import org.netbeans.modules.web.common.spi.ImportantFilesSupport;
2629
import org.netbeans.spi.project.LookupProvider;
2730
import org.netbeans.spi.project.ProjectServiceProvider;
31+
import org.openide.filesystems.FileObject;
2832

2933
@ProjectServiceProvider(service = ImportantFilesImplementation.class, projectTypes = {
3034
@LookupProvider.Registration.ProjectType(id = "org-netbeans-modules-web-clientproject"),
3135
@LookupProvider.Registration.ProjectType(id = "org-netbeans-modules-php-project"),
3236
})
3337
public class EnvFileImpl implements ImportantFilesImplementation {
34-
38+
3539
private final ImportantFilesSupport support;
3640

3741
public EnvFileImpl(Project project) {
3842
assert project != null;
39-
support = ImportantFilesSupport.create(project.getProjectDirectory(), ".env"); // NOI18N
43+
List<String> envFiles = new ArrayList<>();
44+
45+
for (FileObject file : project.getProjectDirectory().getChildren()) {
46+
if (!file.isFolder() && (file.getMIMEType().equals(EnvFileResolver.MIME_TYPE))) {
47+
envFiles.add(file.getNameExt());
48+
}
49+
}
50+
support = ImportantFilesSupport.create(project.getProjectDirectory(), envFiles.toArray(String[]::new));
4051
}
4152

4253
@Override
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
APP_NAME='my file'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Token #0 KEY [APP_NAME]
2+
Token #1 OPERATOR [=]
3+
Token #2 STRING ['my file']
4+
Token #3 WS [\n]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
VALUE1='app\Kernel'
2+
VALUE2='my\file'
3+
INVALID_STRING='my\\'file'
4+
NEXT_VALUE=
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Token #0 KEY [VALUE1]
2+
Token #1 OPERATOR [=]
3+
Token #2 STRING ['app\Kernel']
4+
Token #3 WS [\n]
5+
Token #4 KEY [VALUE2]
6+
Token #5 OPERATOR [=]
7+
Token #6 STRING ['my\file']
8+
Token #7 WS [\n]
9+
Token #8 KEY [INVALID_STRING]
10+
Token #9 OPERATOR [=]
11+
Token #10 STRING ['my\\']
12+
Token #11 VALUE [file']
13+
Token #12 WS [\n]
14+
Token #13 KEY [NEXT_VALUE]
15+
Token #14 OPERATOR [=]

ide/languages.env/test/unit/src/org/netbeans/modules/languages/env/lexer/EnvLexerTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@ public void setUp() throws Exception {
4444
public void testLexer_01() throws Exception {
4545
checkLexer("testfiles/lexer/env01.env");
4646
}
47+
48+
public void testSingleQuotedValue() throws Exception {
49+
checkLexer("testfiles/lexer/single_quoted_value.env");
50+
}
4751

52+
public void testSmokeLexerDotEnvFile() throws Exception {
53+
checkLexer("testfiles/lexer/.env");
54+
}
55+
4856
private void checkLexer(final String filePath) throws Exception {
4957
String fileContent = Files.readString(new File(getDataDir(), filePath).toPath(), StandardCharsets.UTF_8);
5058
EnvLanguage langSettings = new EnvLanguage();

0 commit comments

Comments
 (0)