-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added support for NixOS config files (#229)
* add support for .nix (NixOS) config files * removed nixos provider import of javascript provider function * Fixed leftover mistake from previous commit
- Loading branch information
Showing
10 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { PathIntellisenseProvider } from "./provider.interface"; | ||
import { DefaultProvider } from "./default.provider"; | ||
import { JavaScriptProvider } from "./javascript/javascript.provider"; | ||
import { NixProvider } from './nixos/nixos.provider'; | ||
|
||
export const providers: PathIntellisenseProvider[] = [ | ||
JavaScriptProvider, | ||
NixProvider, | ||
DefaultProvider, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import * as vscode from "vscode"; | ||
import { PathIntellisenseProvider } from "../provider.interface"; | ||
import { getConfiguration } from "../../configuration/configuration.service"; | ||
import { Config, Mapping } from "../../configuration/configuration.interface"; | ||
import { createContext, Context } from "../../utils/createContext"; | ||
import { getChildrenOfPath, getPathOfFolderToLookupFiles } from '../../utils/file-utills'; | ||
import { createPathCompletionItem } from '../../utils/createCompletionItem'; | ||
|
||
export const NixProvider: PathIntellisenseProvider = { | ||
selector: { | ||
pattern: '**/*.nix', | ||
scheme: 'file' | ||
}, | ||
provider: { | ||
provideCompletionItems, | ||
}, | ||
triggerCharacters: ["/"], | ||
}; | ||
|
||
async function provideCompletionItems( | ||
document: vscode.TextDocument, | ||
position: vscode.Position | ||
): Promise<vscode.CompletionItem[]> { | ||
const context = createContext(document, position); | ||
const config = await getConfiguration(document.uri); | ||
|
||
const typedString = getTypedString(context, config); | ||
return typedString === null | ||
? Promise.resolve([]) | ||
: provide(context, config, typedString); | ||
} | ||
|
||
/** | ||
* Returns the currently typed string, prior to the cursor position. | ||
* Should have the same effect as context.fromString, expect that it doesn't | ||
* require string quotations to be present, making it compatible with nix files. | ||
* This is important because in nix, string quoted paths are explicitly forced as absolute, | ||
* a relative path may not use any quotations, making context.fromString return null. | ||
* This function will too return null if it decides a path completion is not appropriate | ||
* at the current cursor position. | ||
* @param context | ||
* @param config | ||
*/ | ||
function getTypedString(context: Context, config: Config): null | string { | ||
const { fromString: contextFromString } = context; | ||
|
||
let noQuoteString: undefined | string = undefined; | ||
|
||
if (!contextFromString) { | ||
const cursorPos = context.importRange.start.character; | ||
const lineUpToCursor = context.textFullLine.slice(0, cursorPos); | ||
noQuoteString = lineUpToCursor.match(/\.{0,2}\/[^\"\'\,\s]*$/)?.[0]; | ||
} | ||
|
||
const fromString = noQuoteString ?? contextFromString; | ||
|
||
if (fromString) { | ||
const startsWithDot = fromString[0] === "."; | ||
const startsWithSlash = fromString[0] === "/"; | ||
const startsWithMapping = config.mappings.some(({ key }) => | ||
fromString.startsWith(key) | ||
); | ||
|
||
return ( | ||
startsWithDot || | ||
startsWithMapping || | ||
(startsWithSlash && config.showOnAbsoluteSlash) | ||
) ? fromString : null; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Provide Completion Items | ||
*/ | ||
async function provide( | ||
context: Context, | ||
config: Config, | ||
directPathString: string | ||
): Promise<vscode.CompletionItem[]> { | ||
const workspace = vscode.workspace.getWorkspaceFolder(context.document.uri); | ||
|
||
const rootPath = | ||
config.absolutePathTo || | ||
(config.absolutePathToWorkspace ? workspace?.uri.fsPath : undefined); | ||
|
||
const path = getPathOfFolderToLookupFiles( | ||
context.document.uri.fsPath, | ||
directPathString, | ||
rootPath, | ||
config.mappings | ||
); | ||
|
||
const childrenOfPath = await getChildrenOfPath( | ||
path, | ||
config.showHiddenFiles, | ||
config.filesExclude | ||
); | ||
|
||
return [ | ||
...childrenOfPath.map((child) => | ||
createPathCompletionItem(child, config, context) | ||
), | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,9 @@ | |
}, | ||
{ | ||
"path": "project-withBaseUrlRoot2" | ||
}, | ||
{ | ||
"path": "project-nixos-config" | ||
} | ||
], | ||
"settings": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
# test of absolute import paths with strings home-assistant import from sibling folder | ||
testPath = [ "/services/" ] | ||
} |
12 changes: 12 additions & 0 deletions
12
src/test/demo-workspace/project-nixos-config/configuration.nix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ config, pkgs, ... }: | ||
|
||
{ | ||
imports = | ||
[ | ||
# test if path completion works without string quotes | ||
./apps/ | ||
] | ||
++ [./services/]; # check if path completion still work even with characters prefixed | ||
|
||
system.stateVersion = "23.11"; | ||
} |
6 changes: 6 additions & 0 deletions
6
src/test/demo-workspace/project-nixos-config/services/home-assistant.nix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ config, pkgs, ... }: | ||
|
||
{ | ||
# path completion for above "test.nix" | ||
test.path = [ ../ ] | ||
} |
Empty file.
4 changes: 2 additions & 2 deletions
4
...viders/javascript/createCompletionItem.ts → src/utils/createCompletionItem.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.