@tsuzuri-lab/sed-language-server 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 tsuzuri-lab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # sed-language-server
2
+
3
+ [![CI](https://github.com/tsuzuri-lab/sed-language-server/actions/workflows/ci.yml/badge.svg)](https://github.com/tsuzuri-lab/sed-language-server/actions/workflows/ci.yml)
4
+
5
+ A [Language Server Protocol](https://microsoft.github.io/language-server-protocol/)
6
+ implementation for
7
+ [POSIX `sed`](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/utilities/sed.html)
8
+ with opt-in GNU sed 4.10 syntax support.
9
+
10
+ The default profile is POSIX with basic regular expressions. BSD and
11
+ other implementation-specific extensions are outside the current scope.
12
+
13
+ ## Features
14
+
15
+ The server provides:
16
+
17
+ - profile-aware diagnostics for supported POSIX and GNU sed syntax
18
+ - profile-aware command, substitute-flag, and branch-label completion
19
+ - go to definition from `b`, `t`, and GNU `T` label references
20
+
21
+ It analyzes scripts without executing them, running shell commands, or
22
+ accessing referenced files.
23
+
24
+ ## Syntax profiles
25
+
26
+ The supported profile values are:
27
+
28
+ - `dialect`: `posix` or `gnu`
29
+ - `regexpMode`: `bre` or `ere`
30
+
31
+ Clients can select a profile during initialization:
32
+
33
+ ```json
34
+ {
35
+ "initializationOptions": {
36
+ "dialect": "gnu",
37
+ "regexpMode": "bre"
38
+ }
39
+ }
40
+ ```
41
+
42
+ They can change it for all open documents through
43
+ `workspace/didChangeConfiguration`:
44
+
45
+ ```json
46
+ {
47
+ "settings": {
48
+ "sedLanguageServer": {
49
+ "dialect": "gnu",
50
+ "regexpMode": "ere"
51
+ }
52
+ }
53
+ }
54
+ ```
55
+
56
+ Omitted settings select `posix` and `bre`. The server does not infer a dialect
57
+ from document contents.
58
+
59
+ ## Development
60
+
61
+ ```sh
62
+ npm ci
63
+ npm run check
64
+ npm test
65
+ npm start
66
+ ```
67
+
68
+ ## License
69
+
70
+ [MIT](LICENSE)
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@tsuzuri-lab/sed-language-server",
3
+ "version": "0.1.0",
4
+ "description": "Language server for POSIX sed with opt-in GNU syntax support.",
5
+ "keywords": [
6
+ "sed",
7
+ "language-server",
8
+ "lsp"
9
+ ],
10
+ "type": "module",
11
+ "files": [
12
+ "src"
13
+ ],
14
+ "bin": {
15
+ "sed-language-server": "src/server.js"
16
+ },
17
+ "scripts": {
18
+ "check": "biome check .",
19
+ "check:write": "biome check --write .",
20
+ "prepublishOnly": "npm run check && npm test",
21
+ "start": "node src/server.js --stdio",
22
+ "test": "node --test"
23
+ },
24
+ "license": "MIT",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/tsuzuri-lab/sed-language-server.git"
28
+ },
29
+ "homepage": "https://github.com/tsuzuri-lab/sed-language-server#readme",
30
+ "bugs": {
31
+ "url": "https://github.com/tsuzuri-lab/sed-language-server/issues"
32
+ },
33
+ "engines": {
34
+ "node": ">=20"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "dependencies": {
40
+ "vscode-languageserver": "^10.1.0",
41
+ "vscode-languageserver-textdocument": "^1.0.12"
42
+ },
43
+ "devDependencies": {
44
+ "@biomejs/biome": "2.5.5"
45
+ }
46
+ }
@@ -0,0 +1,148 @@
1
+ import {
2
+ CompletionItemKind,
3
+ InsertTextFormat,
4
+ } from "vscode-languageserver/node";
5
+ import { getDocumentStructure } from "./document-structure.js";
6
+ import {
7
+ commandSpecificationsFor,
8
+ substituteFlagSpecificationsFor,
9
+ } from "./sed-syntax.js";
10
+ import {
11
+ defaultSyntaxProfile,
12
+ requireSyntaxProfile,
13
+ } from "./syntax-profile.js";
14
+
15
+ function plainCompletion(label, kind, documentation) {
16
+ return {
17
+ label,
18
+ kind,
19
+ insertText: label,
20
+ insertTextFormat: InsertTextFormat.PlainText,
21
+ documentation,
22
+ };
23
+ }
24
+
25
+ const commandCompletionsByProfile = new Map();
26
+ const substituteFlagCompletionsByProfile = new Map();
27
+
28
+ function commandCompletionsFor(syntaxProfile) {
29
+ let completions = commandCompletionsByProfile.get(syntaxProfile);
30
+ if (completions !== undefined) {
31
+ return completions;
32
+ }
33
+
34
+ completions = [
35
+ plainCompletion(
36
+ ";",
37
+ CompletionItemKind.Keyword,
38
+ "Insert an empty command and begin the next command.",
39
+ ),
40
+ ...commandSpecificationsFor(syntaxProfile).map(
41
+ ({ command, documentation }) =>
42
+ plainCompletion(command, CompletionItemKind.Keyword, documentation),
43
+ ),
44
+ ];
45
+ commandCompletionsByProfile.set(syntaxProfile, completions);
46
+ return completions;
47
+ }
48
+
49
+ function substituteFlagCompletionsFor(syntaxProfile) {
50
+ let completions = substituteFlagCompletionsByProfile.get(syntaxProfile);
51
+ if (completions !== undefined) {
52
+ return completions;
53
+ }
54
+
55
+ completions = [
56
+ ...Array.from({ length: 9 }, (_, index) => {
57
+ const occurrence = String(index + 1);
58
+ return plainCompletion(
59
+ occurrence,
60
+ CompletionItemKind.Value,
61
+ `Replace only occurrence number ${occurrence}.`,
62
+ );
63
+ }),
64
+ ...substituteFlagSpecificationsFor(syntaxProfile).map(
65
+ ({ flag, documentation }) =>
66
+ plainCompletion(flag, CompletionItemKind.Keyword, documentation),
67
+ ),
68
+ ];
69
+ substituteFlagCompletionsByProfile.set(syntaxProfile, completions);
70
+ return completions;
71
+ }
72
+
73
+ export const completionProviderOptions = Object.freeze({});
74
+
75
+ function branchLabelEditRange(document, context, offset) {
76
+ const replacementRange = context.replacementRange ?? {
77
+ startOffset: offset,
78
+ endOffset: offset,
79
+ };
80
+ const startOffset = Math.min(replacementRange.startOffset, offset);
81
+ const endOffset = Math.max(replacementRange.endOffset, offset);
82
+
83
+ return {
84
+ start: document.positionAt(startOffset),
85
+ end: document.positionAt(endOffset),
86
+ };
87
+ }
88
+
89
+ export function createCompletions(
90
+ document,
91
+ position,
92
+ options = defaultSyntaxProfile,
93
+ ) {
94
+ const syntaxProfile = requireSyntaxProfile(options);
95
+ const structure = getDocumentStructure(document, syntaxProfile);
96
+ const offset = document.offsetAt(position);
97
+ const context = structure.contextDetailsAt(offset);
98
+
99
+ if (context?.kind === "command") {
100
+ return commandCompletionsFor(syntaxProfile).map((completion) => ({
101
+ ...completion,
102
+ }));
103
+ }
104
+
105
+ if (context?.kind === "substitute-flag") {
106
+ return substituteFlagCompletionsFor(syntaxProfile).map((completion) => ({
107
+ ...completion,
108
+ }));
109
+ }
110
+
111
+ if (context?.kind !== "branch-label") {
112
+ return [];
113
+ }
114
+
115
+ const names = new Set();
116
+ const completions = [];
117
+ const editRange = branchLabelEditRange(document, context, offset);
118
+ for (const definition of structure.labelDefinitions) {
119
+ if (names.has(definition.name)) {
120
+ continue;
121
+ }
122
+
123
+ names.add(definition.name);
124
+ completions.push({
125
+ label: definition.name,
126
+ kind: CompletionItemKind.Reference,
127
+ insertTextFormat: InsertTextFormat.PlainText,
128
+ documentation: "Branch label defined in this document.",
129
+ textEdit: {
130
+ range: editRange,
131
+ newText: definition.name,
132
+ },
133
+ });
134
+ }
135
+ return completions;
136
+ }
137
+
138
+ export function createCompletionHandler(
139
+ documents,
140
+ getSyntaxProfile = () => defaultSyntaxProfile,
141
+ ) {
142
+ return ({ textDocument, position }) => {
143
+ const document = documents.get(textDocument.uri);
144
+ return document === undefined
145
+ ? null
146
+ : createCompletions(document, position, getSyntaxProfile());
147
+ };
148
+ }
@@ -0,0 +1,46 @@
1
+ import { getDocumentStructure } from "./document-structure.js";
2
+ import { defaultSyntaxProfile } from "./syntax-profile.js";
3
+
4
+ export const definitionProvider = true;
5
+
6
+ function containsCursor(range, offset) {
7
+ return range.startOffset <= offset && offset <= range.endOffset;
8
+ }
9
+
10
+ export function createDefinitionLocations(
11
+ document,
12
+ position,
13
+ syntaxProfile = defaultSyntaxProfile,
14
+ ) {
15
+ const structure = getDocumentStructure(document, syntaxProfile);
16
+ const offset = document.offsetAt(position);
17
+ const reference = structure.labelReferences.find(({ range }) =>
18
+ containsCursor(range, offset),
19
+ );
20
+
21
+ if (reference === undefined) {
22
+ return [];
23
+ }
24
+
25
+ return structure.labelDefinitions
26
+ .filter((definition) => definition.name === reference.name)
27
+ .map((definition) => ({
28
+ uri: document.uri,
29
+ range: {
30
+ start: document.positionAt(definition.range.startOffset),
31
+ end: document.positionAt(definition.range.endOffset),
32
+ },
33
+ }));
34
+ }
35
+
36
+ export function createDefinitionHandler(
37
+ documents,
38
+ getSyntaxProfile = () => defaultSyntaxProfile,
39
+ ) {
40
+ return ({ textDocument, position }) => {
41
+ const document = documents.get(textDocument.uri);
42
+ return document === undefined
43
+ ? null
44
+ : createDefinitionLocations(document, position, getSyntaxProfile());
45
+ };
46
+ }