Skip to content

Commit

Permalink
Add GraphQL configuration page for indentation (#29)
Browse files Browse the repository at this point in the history
(cherry picked from commit 6289c01)
  • Loading branch information
jimkyndemeyer committed Oct 30, 2016
1 parent a67868b commit 930389d
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
2 changes: 2 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@
<lang.quoteHandler language="GraphQL" implementationClass="com.intellij.lang.jsgraphql.ide.editor.JSGraphQLQuoteHandler" />
<lang.quoteHandler language="GraphQL Schema" implementationClass="com.intellij.lang.jsgraphql.ide.editor.JSGraphQLQuoteHandler" />
<enterHandlerDelegate implementation="com.intellij.lang.jsgraphql.ide.formatter.JSGraphQLEnterHandlerDelegate" />
<langCodeStyleSettingsProvider implementation="com.intellij.lang.jsgraphql.ide.formatter.JSGraphQLLanguageCodeStyleSettingsProvider"/>
<codeStyleSettingsProvider implementation="com.intellij.lang.jsgraphql.ide.formatter.JSGraphQLCodeStyleSettingsProvider" />


<!-- Find usages -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,14 @@ private static class JSGraphQLCodeStyleMainPanel extends TabbedLanguageCodeStyle
public JSGraphQLCodeStyleMainPanel(CodeStyleSettings currentSettings, CodeStyleSettings settings) {
super(JSGraphQLLanguage.INSTANCE, currentSettings, settings);
}

@Override
protected void addSpacesTab(CodeStyleSettings settings) {}

@Override
protected void addBlankLinesTab(CodeStyleSettings settings) {}

@Override
protected void addWrappingAndBracesTab(CodeStyleSettings settings) {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright (c) 2015-present, Jim Kynde Meyer
* All rights reserved.
* <p>
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.intellij.lang.jsgraphql.ide.formatter;

import com.intellij.application.options.IndentOptionsEditor;
import com.intellij.lang.Language;
import com.intellij.lang.jsgraphql.JSGraphQLLanguage;
import com.intellij.openapi.application.ApplicationBundle;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public class JSGraphQLLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSettingsProvider {

@NotNull
@Override
public Language getLanguage() {
return JSGraphQLLanguage.INSTANCE;
}

@Nullable
@Override
public CommonCodeStyleSettings getDefaultCommonSettings() {
final CommonCodeStyleSettings defaultSettings = new CommonCodeStyleSettings(getLanguage());
CommonCodeStyleSettings.IndentOptions indentOptions = defaultSettings.initIndentOptions();
indentOptions.INDENT_SIZE = 4;
indentOptions.USE_TAB_CHARACTER = false;
return defaultSettings;
}

@Nullable
@Override
public IndentOptionsEditor getIndentOptionsEditor() {
return new IndentOptionsEditor() {
protected void addComponents() {
addTabOptions();
myTabSizeField = new JTextField();
myTabSizeLabel = new JLabel();
myIndentField = createIndentTextField();
myIndentLabel = new JLabel(ApplicationBundle.message("editbox.indent.indent"));
add(myIndentLabel, myIndentField);
}

protected void addTabOptions() {
myCbUseTab = new JCheckBox("");
}
};
}

@Override
public String getCodeSample(@NotNull SettingsType settingsType) {
return "query MyQuery {\n" +
" __schema {\n" +
" types {\n" +
" ...FullType\n" +
" }\n" +
" }\n" +
"}\n" +
"\n" +
"fragment FullType on __Type {\n" +
" # Note: __Type has a lot more fields than this\n" +
" name\n" +
"}\n" +
"\n" +
"mutation MyMutation($input: MyInput!) {\n" +
" # Payload\n" +
"}\n";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,28 @@
package com.intellij.lang.jsgraphql.ide.highlighting;

import com.intellij.lang.jsgraphql.schema.JSGraphQLSchemaFileType;
import com.intellij.openapi.fileTypes.PlainSyntaxHighlighter;
import com.intellij.openapi.fileTypes.SyntaxHighlighter;
import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

public class JSGraphQLSyntaxHighlighterFactory extends SyntaxHighlighterFactory {
@NotNull
@Override
public SyntaxHighlighter getSyntaxHighlighter(Project project, VirtualFile virtualFile) {
final boolean schema = virtualFile.getFileType() == JSGraphQLSchemaFileType.INSTANCE;
final boolean schema = virtualFile != null && virtualFile.getFileType() == JSGraphQLSchemaFileType.INSTANCE;
if(project == null) {
Project[] openProjects = ProjectManager.getInstance().getOpenProjects();
if(openProjects.length > 0) {
project = openProjects[0];
} else {
// can't syntax highlight GraphQL without a project to associate the language service instance to
return new PlainSyntaxHighlighter();
}
}
return new JSGraphQLSyntaxHighlighter(project, schema);
}
}

0 comments on commit 930389d

Please sign in to comment.