aboutsummaryrefslogtreecommitdiffstats
path: root/integration/intellij/src/main/java/ai/vespa/intellij/schema/psi/SdElementFactory.java
blob: d6cbcece6d54f87ac9903a6eef3e04061e6e09ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.intellij.schema.psi;

import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFileFactory;

import com.intellij.psi.util.PsiTreeUtil;
import ai.vespa.intellij.schema.SdFileType;

/**
 * This class is a factory of psi elements in the SD PSI tree.
 *
 * @author Shahar Ariel
 */
public class SdElementFactory {
    
    private static final String GENERAL_FILE_TEXT = "search {document %s {} rank-profile %s {}}";
    
    public static SdIdentifierVal createIdentifierVal(Project project, String name) {
        String fileText = String.format(GENERAL_FILE_TEXT, name, name);
        final SdFile file = createFile(project, fileText);
        return PsiTreeUtil.findChildOfType(file, SdIdentifierVal.class);
    }
    
    public static SdIdentifierWithDashVal createIdentifierWithDashVal(Project project, String name) {
        String fileText = String.format(GENERAL_FILE_TEXT, name, name);
        final SdFile file = createFile(project, fileText);
        return PsiTreeUtil.findChildOfType(file, SdIdentifierWithDashVal.class);
    }
    
    public static SdFile createFile(Project project, String text) {
        String name = "dummy.sd";
        return (SdFile) PsiFileFactory.getInstance(project).
            createFileFromText(name, SdFileType.INSTANCE, text);
    }

}