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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use std::collections::HashMap;

use lsp_types::{
    CompletionItem, CompletionItemKind, CompletionList, Documentation, InsertTextFormat, MarkupContent, MarkupKind,
};
use psl::datamodel_connector::format_completion_docs;

use super::{add_quotes, CompletionContext};

pub(super) fn relation_mode_completion(completion_list: &mut CompletionList) {
    completion_list.items.push(CompletionItem {
        label: "relationMode".to_owned(),
        insert_text: Some(r#"relationMode = $0"#.to_owned()),
        insert_text_format: Some(InsertTextFormat::SNIPPET),
        kind: Some(CompletionItemKind::FIELD),
        documentation: Some(Documentation::MarkupContent(MarkupContent {
            kind: MarkupKind::Markdown,
            value: format_completion_docs(
                r#"relationMode = "foreignKeys" | "prisma""#,
                r#"Set the global relation mode for all relations. Values can be either "foreignKeys" (Default), or "prisma". [Learn more](https://pris.ly/d/relation-mode)"#,
                None,
            ),
        })),
        ..Default::default()
    })
}

pub(super) fn direct_url_completion(completion_list: &mut CompletionList) {
    completion_list.items.push(CompletionItem {
        label: "directUrl".to_owned(),
        insert_text: Some(r#"directUrl = $0"#.to_owned()),
        insert_text_format: Some(InsertTextFormat::SNIPPET),
        kind: Some(CompletionItemKind::FIELD),
        documentation: Some(Documentation::MarkupContent(MarkupContent {
            kind: MarkupKind::Markdown,
            value: format_completion_docs(
                r#"directUrl = "String" | env("ENVIRONMENT_VARIABLE")"#,
                r#"Connection URL for direct connection to the database. [Learn more](https://pris.ly/d/data-proxy-cli)."#,
                None,
            )
        })),
        ..Default::default()
    })
}

pub(super) fn shadow_db_completion(completion_list: &mut CompletionList) {
    completion_list.items.push(CompletionItem {
        label: "shadowDatabaseUrl".to_owned(),
        insert_text: Some(r#"shadowDatabaseUrl = $0"#.to_owned()),
        insert_text_format: Some(InsertTextFormat::SNIPPET),
        kind: Some(CompletionItemKind::FIELD),
        documentation: Some(Documentation::MarkupContent(MarkupContent {
            kind: MarkupKind::Markdown,
            value: format_completion_docs(
                r#"shadowDatabaseUrl = "String" | env("ENVIRONMENT_VARIABLE")"#,
                r#"Connection URL including authentication info to use for Migrate's [shadow database](https://pris.ly/d/migrate-shadow)."#,
                None,
            ),
        })),
        ..Default::default()
    })
}

pub(super) fn url_completion(completion_list: &mut CompletionList) {
    completion_list.items.push(CompletionItem {
        label: "url".to_owned(),
        insert_text: Some(r#"url = $0"#.to_owned()),
        insert_text_format: Some(InsertTextFormat::SNIPPET),
        kind: Some(CompletionItemKind::FIELD),
        documentation: Some(Documentation::MarkupContent(MarkupContent {
            kind: MarkupKind::Markdown,
            value: format_completion_docs(
                r#"url = "String" | env("ENVIRONMENT_VARIABLE")"#,
                r#"Connection URL including authentication info. Each datasource provider documents the URL syntax. Most providers use the syntax provided by the database. [Learn more](https://pris.ly/d/connection-strings)."#,
                None,
            ),
        })),
        ..Default::default()
    })
}

pub(super) fn provider_completion(completion_list: &mut CompletionList) {
    completion_list.items.push(CompletionItem {
        label: "provider".to_owned(),
        insert_text: Some(r#"provider = $0"#.to_owned()),
        insert_text_format: Some(InsertTextFormat::SNIPPET),
        kind: Some(CompletionItemKind::FIELD),
        documentation: Some(Documentation::MarkupContent(MarkupContent {
            kind: MarkupKind::Markdown,
            value: format_completion_docs(
                r#"provider = "foo""#,
                r#"Describes which datasource connector to use. Can be one of the following datasource providers: `postgresql`, `mysql`, `sqlserver`, `sqlite`, `mongodb` or `cockroachdb`."#,
                None,
            ),
        })),
        ..Default::default()
    })
}

pub(super) fn url_env_completion(completion_list: &mut CompletionList) {
    completion_list.items.push(CompletionItem {
        label: "env()".to_owned(),
        insert_text: Some(r#"env($0)"#.to_owned()),
        insert_text_format: Some(InsertTextFormat::SNIPPET),
        kind: Some(CompletionItemKind::PROPERTY),
        documentation: Some(Documentation::MarkupContent(MarkupContent {
            kind: MarkupKind::Markdown,
            value: format_completion_docs(
                r#"env(_ environmentVariable: string)"#,
                r#"Specifies a datasource via an environment variable. When running a Prisma CLI command that needs the database connection URL (e.g. `prisma db pull`), you need to make sure that the `DATABASE_URL` environment variable is set. One way to do so is by creating a `.env` file. Note that the file must be in the same directory as your schema.prisma file to automatically be picked up by the Prisma CLI.""#,
                Some(HashMap::from([(
                    "environmentVariable",
                    "The environment variable in which the database connection URL is stored.",
                )])),
            ),
        })),
        ..Default::default()
    })
}

pub(super) fn url_quotes_completion(completion_list: &mut CompletionList) {
    completion_list.items.push(CompletionItem {
        label: r#""""#.to_owned(),
        insert_text: Some(r#""$0""#.to_owned()),
        insert_text_format: Some(InsertTextFormat::SNIPPET),
        kind: Some(CompletionItemKind::PROPERTY),
        documentation: Some(Documentation::MarkupContent(MarkupContent {
            kind: MarkupKind::Markdown,
            value: format_completion_docs(
                r#""connectionString""#,
                r#"Connection URL including authentication info. Each datasource provider documents the URL syntax. Most providers use the syntax provided by the database. [Learn more](https://pris.ly/d/prisma-schema)."#,
                None,
            ),
        })),
        ..Default::default()
    })
}

pub(super) fn url_env_db_completion(completion_list: &mut CompletionList, kind: &str, ctx: CompletionContext<'_>) {
    let text = match kind {
        "url" => "DATABASE_URL",
        "directUrl" => "DIRECT_URL",
        "shadowDatabaseUrl" => "SHADOW_DATABASE_URL",
        _ => unreachable!(),
    };

    let insert_text = if add_quotes(ctx.params, ctx.db.source()) {
        format!(r#""{text}""#)
    } else {
        text.to_owned()
    };

    completion_list.items.push(CompletionItem {
        label: text.to_owned(),
        insert_text: Some(insert_text),
        insert_text_format: Some(InsertTextFormat::PLAIN_TEXT),
        kind: Some(CompletionItemKind::CONSTANT),
        ..Default::default()
    })
}