in src/main/com/intellij/lang/jsgraphql/ide/highlighting/query/GraphQLQueryContextHighlightVisitor.java [76:178]
public static GraphQLQueryContext getQueryContextBufferAndHighlightUnused(@NotNull Editor editor) {
Project project = editor.getProject();
Document document = editor.getDocument();
if (project != null) {
PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
if (psiFile != null) {
boolean hasSelection = editor.getSelectionModel().hasSelection();
CharSequence buffer = document.getImmutableCharSequence();
int bufferLength = buffer.length();
// if there's a selection we send that to the server, with an error callback that hints
// that placing the caret inside an operation will include any used fragments
if (hasSelection) {
return runQueryForSelection(project, editor, psiFile, buffer, bufferLength);
}
// no selection -- see if the caret is inside an operation
GraphQLOperationDefinition operationAtCursor = getOperationAtOffset(psiFile, editor.getCaretModel().getOffset());
if (operationAtCursor != null) {
Map<String, GraphQLFragmentDefinition> foundFragments = Maps.newHashMap();
findFragmentsInsideOperation(operationAtCursor, foundFragments, null);
Set<PsiElement> queryElements = Sets.newHashSet(foundFragments.values());
queryElements.add(operationAtCursor);
StringBuilder query = new StringBuilder(bufferLength);
for (PsiElement psiElement : psiFile.getChildren()) {
if (psiElement instanceof PsiWhiteSpace) {
if (!queryElements.isEmpty()) {
query.append(psiElement.getText());
}
}
else {
TextRange textRange = psiElement.getTextRange();
String fragmentKey = "";
if (psiElement instanceof GraphQLFragmentDefinition) {
fragmentKey = getFragmentKey((GraphQLFragmentDefinition)psiElement);
}
if (queryElements.contains(psiElement) || foundFragments.containsKey(fragmentKey)) {
queryElements.remove(psiElement);
GraphQLFragmentDefinition fragmentDefinition = foundFragments.get(fragmentKey);
if (fragmentDefinition != null) {
queryElements.remove(fragmentDefinition);
}
query.append(buffer.subSequence(textRange.getStartOffset(), textRange.getEndOffset()));
}
else {
if (!queryElements.isEmpty()) {
// element is not part of the query context so add it as new-lined whitespace
for (int i = textRange.getStartOffset(); i < textRange.getEndOffset(); i++) {
char c = buffer.charAt(i);
if (c == '\n') {
// add new-line to preserve query line numbers for errors etc.
query.append(c);
}
else {
// non-line wrap so add as blank text
query.append(' ');
}
}
}
// tone down to indicate the text wasn't included
highlightUnusedRange(editor, textRange);
}
}
}
// include fragments from other PsiFiles
for (PsiElement queryElement : queryElements) {
query.append("\n\n# ---- fragment automatically included from \"");
query.append(GraphQLPsiUtil.getPhysicalFileName(queryElement.getContainingFile())).append("\" ----\n");
query.append(queryElement.getText());
}
if (operationAtCursor.getNameIdentifier() != null) {
// named operation
showQueryContextHint(
editor,
GraphQLBundle.message(
"graphql.hint.text.executed.named.operation",
getOperationKind(operationAtCursor),
operationAtCursor.getNameIdentifier().getText())
);
}
else {
// anonymous operation
showQueryContextHint(
editor,
GraphQLBundle.message("graphql.hint.text.executed.anonymous.operation", getOperationKind(operationAtCursor))
);
}
return new GraphQLQueryContext(query.toString(), null);
}
}
}
// fallback is the entire buffer
VirtualFile file = FileDocumentManager.getInstance().getFile(document);
if (file != null) {
showQueryContextHint(editor, GraphQLBundle.message("graphql.hint.text.executed.buffer", file.getPresentableName()));
}
return new GraphQLQueryContext(document.getText(), null);
}