-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dmytro Kulieshov <dkuliesh@redhat.com>
- Loading branch information
Dmytro Kulieshov
committed
Jan 11, 2018
1 parent
7aaff22
commit edffd6b
Showing
8 changed files
with
435 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
.../src/main/java/org/eclipse/che/api/languageserver/registry/DefaultLanguageRecognizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright (c) 2012-2017 Red Hat, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.che.api.languageserver.registry; | ||
|
||
import static org.eclipse.che.api.fs.server.WsPathUtils.nameOf; | ||
|
||
import java.util.Set; | ||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
import org.eclipse.che.api.languageserver.shared.model.LanguageDescription; | ||
|
||
/** | ||
* Language recognizer based on default language set. Custom language descriptions created that are | ||
* injected by guice are taken into account prior to default language set. Language recognition is | ||
* based on file name or file extension, so may be improved further in future. | ||
*/ | ||
@Singleton | ||
public class DefaultLanguageRecognizer implements LanguageRecognizer { | ||
|
||
private final Set<LanguageDescription> languages; | ||
private final DefaultLanguages defaultLanguages; | ||
|
||
@Inject | ||
public DefaultLanguageRecognizer( | ||
Set<LanguageDescription> languages, DefaultLanguages defaultLanguages) { | ||
this.languages = languages; | ||
this.defaultLanguages = defaultLanguages; | ||
} | ||
|
||
@Override | ||
public LanguageDescription recognizeByPath(String wsPath) { | ||
Name name = new Name(wsPath); | ||
Extension extension = new Extension(wsPath); | ||
|
||
for (LanguageDescription language : languages) { | ||
if (name.matches(language) || extension.matches(language)) { | ||
return language; | ||
} | ||
} | ||
|
||
for (LanguageDescription language : defaultLanguages.getAll()) { | ||
if (name.matches(language) || extension.matches(language)) { | ||
return language; | ||
} | ||
} | ||
|
||
return UNIDENTIFIED; | ||
} | ||
|
||
@Override | ||
public LanguageDescription recognizeById(String id) { | ||
for (LanguageDescription language : languages) { | ||
if (language.getLanguageId().equals(id)) { | ||
return language; | ||
} | ||
} | ||
|
||
for (LanguageDescription language : defaultLanguages.getAll()) { | ||
if (language.getLanguageId().equals(id)) { | ||
return language; | ||
} | ||
} | ||
|
||
return UNIDENTIFIED; | ||
} | ||
|
||
private static class Name { | ||
private final String name; | ||
|
||
private Name(String wsPath) { | ||
String nameAndExtension = nameOf(wsPath); | ||
int lastDotIndex = nameAndExtension.lastIndexOf('.'); | ||
|
||
this.name = lastDotIndex < 0 ? nameAndExtension : nameAndExtension.substring(0, lastDotIndex); | ||
} | ||
|
||
private boolean matches(LanguageDescription languageDescription) { | ||
return languageDescription.getFileNames().contains(name); | ||
} | ||
} | ||
|
||
private static class Extension { | ||
private final String extension; | ||
|
||
private Extension(String wsPath) { | ||
String nameAndExtension = nameOf(wsPath); | ||
int lastDotIndex = nameAndExtension.lastIndexOf('.'); | ||
|
||
this.extension = lastDotIndex < 0 ? "" : nameAndExtension.substring(lastDotIndex + 1); | ||
} | ||
|
||
private boolean matches(LanguageDescription languageDescription) { | ||
return languageDescription.getFileExtensions().contains(extension); | ||
} | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...ageserver/src/main/java/org/eclipse/che/api/languageserver/registry/DefaultLanguages.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (c) 2012-2017 Red Hat, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.che.api.languageserver.registry; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import javax.inject.Singleton; | ||
import org.eclipse.che.api.languageserver.shared.model.LanguageDescription; | ||
|
||
/** Contains set of default language descriptions */ | ||
@Singleton | ||
public class DefaultLanguages { | ||
|
||
private final Set<LanguageDescription> languages = new HashSet<>(); | ||
|
||
public DefaultLanguages() { | ||
register("bat", "bat", "text/plain"); | ||
register("bibtex", "bib", "text/plain"); | ||
register("clojure", "clj", "text/x-clojure"); | ||
register("coffeescript", "coffee", "text/x-coffeescript"); | ||
register("c", "c", "text/x-csrc"); | ||
register("cpp", "cpp", "text/x-c++src"); | ||
register("csharp", "cs", "text/x-csharp"); | ||
register("css", "css", "text/css"); | ||
register("diff", "diff", "text/x-diff"); | ||
register("fsharp", "fs", "text/x-fsharp"); | ||
register("go", "go", "text/x-go"); | ||
register("groovy", "groovy", "text/x-groovy"); | ||
register("handlebars", "hbs", "text/plain"); | ||
register("html", "html", "text/html"); | ||
register("ini", "ini", "text/plain"); | ||
register("jade", "jade", "text/x-jade"); | ||
register("java", "java", "text/x-java"); | ||
register("javascript", "js", "text/javascript"); | ||
register("json", "json", "application/json"); | ||
register("scala", "scala", "text/x-scala"); | ||
register("kt", "kotlin", "text/x-kotlin"); | ||
register("latex", "tex", "text/x-latex"); | ||
register("lisp", "lisp", "text/x-commonlisp"); | ||
register("lua", "lua", "text/x-lua"); | ||
register("makefile", "Makefile", "text/plain"); | ||
register("markdown", "markdown", "text/x-markdown"); | ||
register("objective-c", "m", "text/x-objective-c"); | ||
register("objective-cpp", "mm", "text/x-objective-cpp"); | ||
register("perl", "pl", "text/x-perl"); | ||
register("php", "php", "text/x-php"); | ||
register("powershell", "ps1", "text/plain"); | ||
register("pascal", "pas", "text/x-pascal"); | ||
register("python", "py", "text/x-python"); | ||
register("r", "r", "text/x-rsrc"); | ||
register("ruby", "rb", "text/x-ruby"); | ||
register("rust", "rs", "text/x-rustsrc"); | ||
register("shellscript", "sh", "text/x-sh"); | ||
register("sql", "sql", "text/x-sql"); | ||
register("swift", "swift", "text/x-swift"); | ||
register("typescript", "ts", "application/typescript"); | ||
register("xml", "xml", "application/xml"); | ||
register("xsl", "xsl", "text/plain"); | ||
register("yaml", "yaml", "text/x-yaml"); | ||
|
||
registerWithName("dockerfile", "Dockerfile", "text/x-dockerfile"); | ||
} | ||
|
||
public Set<LanguageDescription> getAll() { | ||
return Collections.unmodifiableSet(languages); | ||
} | ||
|
||
private void register(String id, String extension, String mimeType) { | ||
LanguageDescription languageDescription = new LanguageDescription(); | ||
languageDescription.setLanguageId(id); | ||
languageDescription.setMimeType(mimeType); | ||
languageDescription.setFileExtensions(Collections.singletonList(extension)); | ||
languages.add(languageDescription); | ||
} | ||
|
||
private void registerWithName(String id, String fileName, String mimeType) { | ||
LanguageDescription languageDescription = new LanguageDescription(); | ||
languageDescription.setLanguageId(id); | ||
languageDescription.setMimeType(mimeType); | ||
languageDescription.setFileNames(Collections.singletonList(fileName)); | ||
languages.add(languageDescription); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...eserver/src/main/java/org/eclipse/che/api/languageserver/registry/LanguageRecognizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) 2012-2017 Red Hat, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.che.api.languageserver.registry; | ||
|
||
import org.eclipse.che.api.languageserver.shared.model.LanguageDescription; | ||
|
||
/** | ||
* The implementations of this interface are responsible for language recognition of a file under | ||
* defined path. | ||
*/ | ||
public interface LanguageRecognizer { | ||
LanguageDescription UNIDENTIFIED = | ||
new LanguageDescription() { | ||
@Override | ||
public String getLanguageId() { | ||
return "unidentified"; | ||
} | ||
|
||
@Override | ||
public String getMimeType() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public String getHighlightingConfiguration() { | ||
return ""; | ||
} | ||
}; | ||
|
||
/** | ||
* Recognize a language by file path. If language cannot be recognized for any reason than the | ||
* implementation must return {@link LanguageRecognizer#UNIDENTIFIED}. | ||
* | ||
* @param wsPath workspace path of a file | ||
* @return description of language that was recognized | ||
*/ | ||
LanguageDescription recognizeByPath(String wsPath); | ||
|
||
/** | ||
* Recognize a language by it's identifier. If language cannot be recognized for any reason than | ||
* the implementation must return {@link LanguageRecognizer#UNIDENTIFIED}. | ||
* | ||
* @param id language identifier | ||
* @return description of language that was recognized | ||
*/ | ||
LanguageDescription recognizeById(String id); | ||
} |
Oops, something went wrong.