Skip to content

Commit

Permalink
Bug fixes and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Shabouta committed Aug 15, 2019
1 parent 3232d26 commit 08d1030
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

[ico-version]: https://img.shields.io/jetbrains/plugin/v/intellij-redoc?style=flat-square
[ico-version]: https://img.shields.io/jetbrains/plugin/v/12822-redoc?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square

[link-redoc]: https://github.com/Redocly/redoc
Expand Down
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ javafx {
}

publishPlugin {
token pluginToken
token intellijPluginToken
}

patchPluginXml {
changeNotes """
Refactoring and CS fixes
Swagger 2.0 specification support
Refactoring
CS fixes
"""
}
46 changes: 32 additions & 14 deletions src/main/java/com/phpinnacle/redoc/RedocApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@
import org.jetbrains.yaml.psi.impl.YAMLDocumentImpl;
import org.jetbrains.yaml.psi.impl.YAMLKeyValueImpl;

import java.util.Objects;
import java.util.*;

class RedocApplication implements Disposable {
private RedocServer server = RedocServer.getInstance();
private RedocSettings settings = RedocSettings.getInstance();
private FileDocumentManager manager = FileDocumentManager.getInstance();
private Application application = ApplicationManager.getApplication();

private Map<String, String> openApiFields = new HashMap<>();

RedocApplication() {
openApiFields.put("swagger", "2.0");
openApiFields.put("openapi", "3.0.0");
}

boolean isAcceptable(@NotNull Project project, @NotNull VirtualFile file) {
PsiFile psiFile = PsiManager.getInstance(project).findFile(file);

Expand All @@ -46,11 +53,11 @@ boolean isAcceptable(@NotNull Project project, @NotNull VirtualFile file) {
@NotNull
FileEditor createEditor(@NotNull VirtualFile file) {
RedocEditor editor = new RedocEditor(server, settings, file.getPath());
Document document = manager.getDocument(file);
Document document = Objects.requireNonNull(manager.getDocument(file));

editor.setup(Objects.requireNonNull(document));
editor.setup(document);

server.attach(file.getPath(), document.getText());
server.attach(file.getPath(), document);

application.getMessageBus()
.connect(editor)
Expand All @@ -76,11 +83,13 @@ private boolean isAcceptableJSON(@NotNull PsiFile psiFile) {
continue;
}

JsonProperty schema = ((JsonObjectImpl) element).findProperty("openapi");
JsonValue value = schema != null ? schema.getValue() : null;
for (String key : openApiFields.keySet()) {
JsonProperty property = ((JsonObjectImpl) element).findProperty(key);
JsonValue value = property != null ? property.getValue() : null;

if (value != null) {
return value.textMatches("\"3.0.0\"");
if (value != null) {
return matchValue(value, key);
}
}
}

Expand Down Expand Up @@ -110,14 +119,16 @@ private boolean isAcceptableYAML(@NotNull PsiFile psiFile) {
continue;
}

if (!"openapi".equals(child.getName())) {
continue;
}
for (String key : openApiFields.keySet()) {
if (!key.equals(child.getName())) {
continue;
}

YAMLValue value = ((YAMLKeyValueImpl) child).getValue();
YAMLValue value = ((YAMLKeyValueImpl) child).getValue();

if (value != null) {
return value.textMatches("3.0.0");
if (value != null) {
return matchValue(value, key);
}
}
}
}
Expand All @@ -131,4 +142,11 @@ public void dispose() {

server.dispose();
}

private boolean matchValue(PsiElement element, String key)
{
String version = openApiFields.get(key);

return element.textMatches(version) || element.textMatches("\"" + version + "\"");
}
}
13 changes: 7 additions & 6 deletions src/main/java/com/phpinnacle/redoc/RedocServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.intellij.openapi.Disposable;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.Disposer;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
Expand Down Expand Up @@ -29,8 +30,8 @@ public static RedocServer getInstance() {
return ServiceManager.getService(RedocServer.class);
}

void attach(@NotNull String spec, @NotNull String text) {
handler.attach(spec, text);
void attach(@NotNull String spec, @NotNull Document document) {
handler.attach(spec, document);
}

void detach(@NotNull String spec) {
Expand All @@ -50,10 +51,10 @@ public void dispose() {
}

private static class RootHandler implements HttpHandler {
private Map<String, String> documents = new HashMap<>();
private Map<String, Document> documents = new HashMap<>();

void attach(@NotNull String spec, @NotNull String text) {
documents.put(spec, text);
void attach(@NotNull String spec, @NotNull Document document) {
documents.put(spec, document);
}

void detach(@NotNull String spec) {
Expand All @@ -77,7 +78,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
String path = httpExchange.getRequestURI().getPath();

if (documents.containsKey(path)) {
text = documents.get(path);
text = documents.get(path).getText();
} else {
status = 404;
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@

<description><![CDATA[
OpenAPI 3.0 (Swagger) documentation render via <a target="_blank" href="https://github.com/Rebilly/ReDoc" >Redoc JS library</a>
<h3>Features:</h3>
<ol>
<li>OpenAPI 3.0 and Swagger 2.0 specifications</li>
<li>Specifications in YAML or JSON</li>
<li>Redoc settings supported</li>
</ol>
]]></description>

<idea-version since-build="182"/>
Expand Down

0 comments on commit 08d1030

Please sign in to comment.